【问题标题】:MVC Ajax.Begin form isn't posting back to actionresultMVC Ajax.Begin 表单没有回发到 actionresult
【发布时间】:2011-11-16 20:36:38
【问题描述】:

我最近将以下代码从使用常规 html 'form' 更改为使用 ajax.beginform,但它不会回发到 'PostCheckListCompleted' actionresult。任何想法为什么这不起作用?

        @Html.Grid(Model.ContactApplicationConditionList).RowAttributes(row => new Hash(@class => row.Item.IsSatisfied ? "completeRow" : "incompleteRow")).Columns(c =>
        {
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateRequested)).Named("Date Requested");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateDeadline)).Named("Deadline Date");
            c.For(a => string.Format("{0:dd/MM/yyyy}", a.DateReceived)).Named("Date Received");
            c.For(a => a.Comment).Named("Comment");
            c.For(a => a.ApplicationOfferCondition.Name).Named("Checklist type");
            c.For(a => @Html.Raw(String.Format("<a href=\"#?w=380\" onclick=\"$('body').data('contactApplicationConditionId', '{0}' );\" rel=\"checklistUpdatePopup\" class=\"popupWindow\">Update</a>", a.ContactApplicationConditionId))).Named("Update");
            c.Custom(



                @<text>
                    @using (Ajax.BeginForm("PostCheckListCompleted", "Home", new AjaxOptions  { HttpMethod = "Post" }))
                    {
                        @Html.CheckBox("checkListCompleted", item.IsSatisfied, new { onclick = "$(this).parent('form:first').submit();" })
                        <input type="hidden" name="contactApplicationId" value=@ViewBag.ApplicationId />      
                        <input type="hidden" name="contactApplicationConditionId" value="@item.ContactApplicationConditionId" />     
                    }
                </text>

            ).Named("Complete");
        }).Attributes(@cellpadding => "0", @cellspacing => "0", @class => "table_results")

提前致谢。

【问题讨论】:

  • 您是否尝试过使用 firebug 的 XHR 控制台查看操作返回的内容?
  • 是的,但我没有看到任何活动。我也试过用@Html.SubmitButton 测试它,但没有。我敢肯定它一定很简单,但看不到:o)。
  • 您是否包含了必要的 javascript 文件?
  • Ajax.BeginForm 不应该发布到“PostCheckListAsync”而不是“PostCheckListCompleted”吗?你有 Async 和 Completed for Action 这两种方法吗?
  • Tim:好问题,但我知道 'onclick = "$(this).parent('form:first').submit();"' 有效,因为我在 HTML 中使用了它.form 之前,所以所有必要的文件都包括在内。我也有在其他部分视图中工作的 Ajax 回发。

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-2 asp.net-ajax


【解决方案1】:

您是否引用了 ajax 所需的脚本文件?

Microsoft.Ajax.js
MicrosoftMvc.Ajax.js

如果需要,可能还有 jquery

【讨论】:

    【解决方案2】:

    您需要将 jquery unobtrusive-ajax 库添加到您的页面

    <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
    

    并用

    更改该代码行
    @using (Ajax.BeginForm("PostCheckListCompleted", "Home", new AjaxOptions  { HttpMethod = "Post", Url = Url.Action("PostCheckListCompleted") })) 
    

    还要检查是否要为 ajax 回调设置目标,设置 AjaxOptions 的属性 UpdateTargetId

    【讨论】:

      猜你喜欢
      • 2017-01-09
      • 2018-01-24
      • 2015-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-13
      相关资源
      最近更新 更多