2013-04-07 67 views
1

我有以下表單,我將項目添加到列表並更新結果表格。所以這裏是代碼我目前有:@ Ajax.ActionLink帖子表格字段

視圖(強類型):

@using (Ajax.BeginForm("AddService", "Manager", 
      new AjaxOptions { UpdateTargetId = "servicePartial", 
          HttpMethod = "Post", 
          LoadingElementId = "loading" })) 
{ 
     <input type="submit" value="Add New" />     
     <div class="widget-content table-container" id="servicePartial"> 
      @Html.Partial("_Services", Model) 
     </div> 
} 

部分:

@model Project.Model.ServicesViewModel 
<table id="demo-dtable-02" class="table table-striped"> 
<thead> 
    <tr> 
     <th>Service</th> 
     <th>Price</th> 
     <td class="action-col">Actions</td> 
    </tr> 
</thead> 
<tbody id="services"> 
    @if (Model != null) 
    { 
     if (Model.Services != null) 
     { 
      if (Model.Services.Count > 0) 
      { 
       for (int i = 0; i < Model.Services.Count; i++) 
       { 
        <tr> 
      <th>@Model.Services[i].name</th> 
      <th>@Model.Services[i].price</th> 
      <td class="action-col"> 
       @Html.HiddenFor(x=>x.Services[i].name) 
       @Html.HiddenFor(x=>x.Services[i].price) 
       @Html.HiddenFor(x=>x.Services[i].serviceId) 
       @Ajax.ActionLink(" X ", "Appointment", "Manager", 
           new{ model = Model, 
             id = @Model.Services[i].serviceId }, 
             new AjaxOptions 
             { 
              UpdateTargetId = "servicePartial", 
              LoadingElementId = "loading", 
              HttpMethod="Post" 
             }) 
      </td> 

     </tr> 
       }     
      } 
     } 

    } 
</tbody> 
</table> 

視圖模型:

public class ServicesViewModel 
{ 
    public List<Service> Services { get; set; } 
} 

現在,通過Ajax.Beginform的提交按鈕回覆作品,我可以回發模型並更新服務(添加一個)並返回。

我的問題是,我想能夠從列表中刪除一個服務。爲此,我認爲我會使用Ajax.Actionlink,並使用ID回發。問題是它只返回id而不是模型。

現在看看它顯然不可能發送當前模型回到使用Ajax.Actionlink的行動你將如何處理這個?

我以爲我會很聰明,並使用Ajax.Actionlink的超負荷,並添加 new{ model = Model, id = @Model.Services[i].serviceId }它,但它是一個不行。

ViewModel中有很多其他數據(對於我已經將它從上面的代碼中取出的空間),並且對於我通過上述過載逐個發送所有這些數據是非常不切實際的。

+0

你打算如何刪除該項目?你可以發佈行動方法嗎? – Sharun 2013-04-08 04:51:07

回答

1

您可以使用form.serialize方法將整個表單發佈到使用ajax調用的操作。不要使用操作鏈接,請嘗試按鈕。在該按鈕的onclick事件中,序列化表單並提交。

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

    $.ajax({ 
     type: "POST", 
     url:'@Url.Action("deleteActionName")', 
     data: $('form').serialize(), 
     success: function(response) { 
     console.log(response); 
     } 
    }); 
    }); 
+0

嗨,感謝您的幫助。然而,這個解決方案不會自行工作,因爲我需要發回該行的id(該按鈕處於打開狀態),以便該操作將用於從ViewModel中刪除該服務。 Delete按鈕位於表格的每一行,並且我可以訪問serviceId。再次感謝 – hjavaher 2013-04-08 07:01:52

+0

在這種情況下,您可以將ID保存在隱藏字段中,並將其與表單一起提交。 – Sharun 2013-04-08 08:54:43