【问题标题】:.Net Ajax BeginForm callback not fired.Net Ajax BeginForm 回调未触发
【发布时间】:2013-02-16 14:47:10
【问题描述】:

更新 2:

jQuery 版本兼容性问题。

更新 1:

我的脚本参考中有错字。但是我现在遇到了这个问题:

TypeError: $(...).live is not a function

$("a[data-ajax=true]").live("click", function (evt) {

======================================

当我通过 ajax.beginform 提交有效表单时,在控制器中,它返回的 JSON 显示在视图中,而不是由回调函数处理,我不知道这是为什么。

我从网上拿了一个演示项目,以防万一有人觉得它很熟悉。

HTML/JS

@model Unobtrusive_Validation.Models.BlogPost

<html>
    <head>
    </head>
    <body>
        <script src="~/Scripts/jquery-1.9.1.js"></script>
        <script src="~/Scripts/jquery.validate.js"></script>
        <script src="~/Scripts/jquery.validate.unobtrusive.js"></script>
        <script src="~/Scripts/jquery.unobtrusive.ajax.js"></script>

        @using (Ajax.BeginForm("Test", "BlogPost",
            new AjaxOptions{
                HttpMethod = "POST",
                OnSuccess = "OnSuccess",
                OnBegin = "alert('OnBegin')",
                OnComplete = "alert('OnComplete')",
                OnFailure = "alert('OnFailure')"
            } )
        )
        {
            @Html.ValidationSummary(true)

            <fieldset>
                <legend>BlogPost</legend>

                <div class="editor-label">
                    @Html.LabelFor(model => model.PostedOn)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.PostedOn)
                    @Html.ValidationMessageFor(model => model.PostedOn)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Title)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Title)
                    @Html.ValidationMessageFor(model => model.Title)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Content)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Content)
                    @Html.ValidationMessageFor(model => model.Content)
                </div>

                <div class="editor-label">
                    @Html.LabelFor(model => model.Category)
                </div>
                <div class="editor-field">
                    @Html.EditorFor(model => model.Category)
                    @Html.ValidationMessageFor(model => model.Category)
                </div>

                <p>
                    <input type="submit" value="Create" />
                </p>
            </fieldset>
        }
    </body>

    <script type='text/javascript'>
        function OnSuccess(result) {
            console.log(result);
            if (result.success == false) {
                alert("failed");
            }
        }
    </script>
</html>

控制器:

public class BlogPostController : Controller
{
    // GET: /BlogPost/Create

    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Test(BlogPost _model)
    {
        return Json(new {success = false} );
    }
}

Web.config

<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

【问题讨论】:

    标签: asp.net-mvc unobtrusive-validation


    【解决方案1】:
    <script src="~/Scripts/jquery.unobtrusive.ajax.js"></script>
    

    应该是:

    <script src="~/Scripts/jquery.unobtrusive-ajax.js"></script>
    

    如果您使用了 javascript 调试工具(例如 FireBug 或 Chrome 开发人员工具栏)并查看了“网络”选项卡,那么当浏览器尝试检索由于输入错误而指定的不存在文件时,您会立即看到 404 错误.

    我还建议您将所有 javascript 文件放在 DOM 的末尾,就在结束 &lt;/body&gt; 标记之前。

    故事的寓意:使用 javascript 调试工具,例如 FireBug 或 Chrome 开发人员工具栏。我无法想象一个人在不使用这样的工具的情况下进行 Web 开发。


    更新:

    还要记住,在 jQuery 1.9 中,.live() method has been removed 是重大更改之一。不幸的是 ASP.NET MVC 的 unobtrusive-ajax 脚本依赖于这个函数。所以如果你想在 MVC 中使用一些不显眼的 javascript 特性,你可能需要使用旧版本的 jQuery。

    【讨论】:

    • 啊,是的...错字,谢谢。我现在遇到一个不同的问题,我将编辑我的原始帖子。
    • 如果我没记错的话,ASP.NET MVC jquery.unobtrusive-ajax.js 与 jquery 1.9.1 不兼容。尝试使用与 ASP.NET MVC 不显眼的验证脚本兼容的旧版本 jQuery。其中一项重大更改是.live() method was removed。不幸的是,ASP.NET MVC 不显眼的 ajax 脚本依赖于它。
    • 好的,我会尝试降级的,再次感谢。 (我会尽可能标记为答案)
    【解决方案2】:

    是的

    jquery.unobtrusive-ajax.js
    与 jquery 1.9+ 的更高版本不兼容

    要使用它,请添加 jquery-migrate: Migrate old jQuery code to jQuery 1.9+ js files

    &gt

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 2011-09-30
      • 2012-05-04
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2012-03-10
      相关资源
      最近更新 更多