2012-09-17 38 views
0

我有一個窗體在頁面頂部有一個輸入部分,在下半部分它顯示了添加的對象列表。我需要能夠編輯和刪除這些對象,我不確定從哪裏開始或如何去做。在Asp.Net MVC中刪除和更新對象列表4

此代碼顯示對象列表。

   @if (Model.ListOfRecipients != null) 
      { 
       for (int i = 0; i < Model.ListOfRecipients.Count; i++) 
       { 

       <div class='recipient-wrapper'> 
        <div class='decision_block'> 
         <table class='recipient'> 
          <tr> 
           <td class='recipient-title'> 
@Html.HiddenFor(model=>model.ListOfRecipients[i].RecipientId) 
            <h3> 
             @Html.DisplayTextFor(model => model.ListOfRecipients[i].RecipientName) 
             @Html.HiddenFor(model => model.ListOfRecipients[i].RecipientName) 
            </h3> 

            <div class='delivery-type'> 
             Delivery Type: @Html.DisplayTextFor(model => model.ListOfRecipients[i].DeliveryType) 
                 @Html.HiddenFor(model => model.ListOfRecipients[i].DeliveryType) 
            </div> 
           </td> 
           <td class='na express'> 
            @Html.CheckBoxFor(model => model.ListOfRecipients[i].ExpressIndicator) 
            @Html.HiddenFor(model => model.ListOfRecipients[i].ExpressIndicator) 
           </td> 
           <td class='quantity'> 
            <h3> 
             Qty @Html.DisplayTextFor(model => model.ListOfRecipients[i].Quantity) 
              @Html.HiddenFor(model => model.ListOfRecipients[i].Quantity) 
            </h3> 
           </td> 
           <td class='action'> 
            <input class='button edit_recipient' type='button' value='Edit' /> 
            <input class='button delete_recipient' type='button' value='Delete' /> 
           </td> 
          </tr> 
         </table> 

          <input class='button update_recipient' type='button' value='Update' /> 
          <a class='cancel_update' href='#'>Cancel</a> 
         </div> 
        </div> 
       </div> 

       } 
      } 

回答

0

您需要在控制器中編輯和刪除操作方法,然後設置兩個按鈕以調用相應的方法。

操作方法的代碼將取決於幾個可能需要廣泛討論的因素。

使用剃刀語法 - 對不起別人使用錯誤標記:(

<input class='button edit_recipient' type='button' value='Edit' 
     onclick="location.href='@Url.Action("Edit", "Controller", new { id = Model.Id })'" /> 
+0

我已經添加在拍攝對象ID作爲參數控制器的刪除方法(我已經添加了對象ID作爲一個隱藏的領域不認爲它在我的代碼上面)我如何得到刪除按鈕發送正確的對象的ID? –

+0

我想我需要這樣的刪除@ Html.ActionLink(「收件人」 ,MVC.Recipients.Delete(model => model.ListOfRecipients [i] .RecipientId) –

+0

@RonaldMcDonald編輯我的答案,包含一個參數 –

0

您可以創建一個刪除控制器和一個編輯控制器和上述觀點有其臨危對象ID每一個具有刪除和編輯旁邊,每個按鈕去它自己的觀點,他們可以編輯或刪除