mvc操作方法调用begin form方法,即使它将json返回给另一个操作方法

mvc操作方法调用begin form方法,即使它将json返回给另一个操作方法,json,asp.net-mvc-4,c#-4.0,asp.net-ajax,Json,Asp.net Mvc 4,C# 4.0,Asp.net Ajax,嗨,我目前正在MVC Razor上工作。下面是一个场景。有人能找到解决办法吗?我在Begin有一个网络网格 @using(Html.BeginForm("AllocationOnlyEdit", "AllocationModel", FormMethod.Post, new { block = @Model.block })) { if (Model != null) { var grid = new WebGrid(rowsPerPage: Model.RecordsPerP

嗨,我目前正在MVC Razor上工作。下面是一个场景。有人能找到解决办法吗?我在Begin有一个网络网格

 @using(Html.BeginForm("AllocationOnlyEdit", "AllocationModel", FormMethod.Post, new {
  block = @Model.block
 })) {
  if (Model != null) {
   var grid = new WebGrid(rowsPerPage: Model.RecordsPerPage);
   grid.Bind(Model.AllocModelsListView, autoSortAndPage: false, rowCount: Model.NumberOfRecords); < div class = "wrap" >
    < div id = "gridContent" >
    @grid.GetHtml(
     htmlAttributes: new {
      id = "gdvsearch"
     },
     fillEmptyRows: false,
     tableStyle: "mGrid",
     columns: new [] {
      grid.Column(header: null, canSort: false, format: @ < text > < input id = "ChkDelete_@item.AllocId"
        onclick = "javascript: DeleteCheckBox(this)"
        name = "ChkDelete_@item.AllocId"
        class = "delete-chkbox"
        type = "checkbox"
        value = "@item.AllocId" / > < /text>),
        grid.Column("AllocYear", header: "Year", format: @ < input name = "AllocYear_@item.AllocId"
         id = "AllocYear"
         type = "text"
         style = "font-weight:bold;width:50px;"
         value = "@item.AllocYear"
         disabled = "disabled"
         title = "@item.AllocYear"
         class = "editY-mode" / > ),

        grid.Column("AllocPercentage", header: "Percentage", format: @ < input name = "AllocPercentage_@item.AllocId"
         id = "AllocPercentage_@item.AllocId"
         type = "text"
         style = "font-weight:bold;width:50px;"
         value = "@item.AllocPercentage"
         disabled = "disabled"
         title = "@item.AllocPercentage"
         class = "editPer-mode" / > ),
        grid.Column("LocationDesc", header: "Location", format: @ < input name = "LocationDesc_@item.AllocId"
         id = "LocationDesc_@item.AllocId"
         type = "text"
         style = "font-weight:bold;width:50px;"
         value = "@item.LocationDesc"
         disabled = "disabled"
         title = "@item.LocationDesc"
         class = "editPer-mode" / > ) 
如果要删除3行并添加1行,则应调用操作方法“DeleteAndSaveAtSameTimeModels”4次,然后调用默认路由,即搜索操作方法并返回更新的网格

但它所做的是调用BeginForm操作方法“AllocationOnlyEdit”,然后停止。 action方法返回

return Json(new { ok = true, AllocId = AllocId, y = y, newRows = newRows, sumtrans = sumtrans, tempsumtrancs = tempsumtrancs, result = Url.Action("AllocationModelSearch", objAllocationModels) }, JsonRequestBehavior.AllowGet);

我的假设是,您的保存按钮位于表单内部,当您单击该按钮时,它正在执行正常的表单提交行为。在进行ajax调用之前,应该防止默认的表单提交行为。您可以使用jQuery
preventDefault
方法来执行此操作

$(function(){
  $("#YourButtonId").click(function(e){
     e.preventDefault();   // This line does it !
     //Now continue with your ajax code here
     // $.ajax({});
  });
});

另外,我建议使用POST而不是GET,因为GET对通过查询字符串传递的数据量有限制。

谢谢。。我尝试了上面的代码,它对我有效。但是当我把代码升级到服务器上时,它就不能像预期的那样工作了。删除操作删除了所有记录,而不是更新少数记录,并删除了选中的项目。服务器配置与开发系统相同。您最初的问题是,为什么它要发布到
AllocationOnlyEdit
操作,而不是发布到ajax代码中提到的操作。现在你问的是一个完全不同的问题。我不知道你的代码在服务器端做了什么,你也没有在问题中表现出来。请发布一个带有相关代码的新问题。不要在对答案的评论中提出新问题。
$(function(){
  $("#YourButtonId").click(function(e){
     e.preventDefault();   // This line does it !
     //Now continue with your ajax code here
     // $.ajax({});
  });
});