2012-08-03 5 views
0

편집하려는 표에 항목 목록이 있습니다. 그래서이 EditorFor InboundTransactions 위해 안으로 넣어마크 업에서 컬렉션에 항목을 추가하는 방법은 무엇입니까?

@model Transaction 

    <tr> 
     <td> 
      @Html.EditorFor(x => x.TransactionNumber) 
     </td> 
     <td> 
      <a class="removeLink">Remove</a> 
     </td> 
    </tr> 

:처럼 보이는

@model BankAccount 

@Html.HiddenFor(x => x.AccountId, new { @id="accountId" }) 

<table id="transactionTable"> 
    <tbody> 
     @Html.EditorFor(x => x.InboundTransactions) 
    </tbody> 
</table> 
<a id="addTransactionLink">Add</a> 

<script type="text/javascript"> 
    $(document).ready(function() { 

     $('#transferTable').on('click', 'a.removeLink', function (e) { 
      $(this).closest('tr').remove(); 
     }); 

     $('#addTransactionLink').click(function() { 

      var transaction = { 
       TransactionNumber: "234" 
      }; 

       $.post('/BankAccount/AddRow/', transaction, function (data) { 

       $('#transactionTable').append(data); 
      }); 
     }); 
    }); 
</script> 

Transaction.cshtml :

나는처럼 보이는 파일 BankAccount.cshtml 있습니다. BankAccountController 방법에

:

[HttpPost] 
     public PartialViewResult AddRow(Transaction transaction) 
     { 
      return PartialView("EditorTemplates/Transaction", transaction); 
     } 

그래서 행 추가 버튼을 클릭하면 새 행이 추가되는 일이되어 있는지.

어쨌든 내가 페이지로 이동하고 이미 두 개의 트랜잭션을 가질 수 있다면 그때는 마크 업에서 다음과 같을 수 행을 추가 :

<tr> 
    <td> 
     <input id="BankAccount_Transactions_0__TransactionNumber" class="text-box single-line" type="text" value="456" name="BankAccount.Transactions[0].TransactionNumber" data-val-required="The TransactionNumber field is required." data-val-number="The field TransactionNumber must be a number." data-val="true"> 
    </td> 
    <td> 
     <a class="removeLink">Remove</a> 
    </td> 
</tr> 

<tr> 
    <td> 
     <input id="BankAccount_Transactions_1__TransactionNumber" class="text-box single-line" type="text" value="456" name="BankAccount.Transactions[1].TransactionNumber" data-val-required="The TransactionNumber field is required." data-val-number="The field TransactionNumber must be a number." data-val="true"> 
    </td> 
    <td> 
     <a class="removeLink">Remove</a> 
    </td> 
</tr> 

<tr> 
    <td> 
    <input id="Transactions" class="text-box single-line" type="text" value="0" name="Transactions"> 
    </td> 
    <td> 
    <a class="removeLink">Remove</a> 
    </td> 
</tr> 

그래서 당신이 여기에 명백한 문제를 볼 수 있습니다. 새로 추가 된 행에 BankAccount에 속한 트랜잭션 콜렉션의 일부임을 나타내는 정보가 없습니다.

포스트 백이 발생하면 새로 추가 된 행은 포함되지 않습니다.

누구나이 작업 방법을 알고 있습니까?

답변

0

가변 길이 목록 편집을 위해 following article을 살펴 보시기 바랍니다. Guides를 사용하여 콜렉션의 번호가 매겨진 인덱스를 대체하는 사용자 정의 BeginCollectionItem 도우미의 사용법을 보여줍니다. 인덱스를 다시 동기화해야하기 때문에 나중에 항목을 제거하고 추가 할 때 도움이됩니다. Guides에는 그런 문제가 없습니다.

관련 문제