2

我长期以来一直对 Ajax.BeginForm 有问题是在互联网上进行搜索,但是大多数包含jquery.unobtrusive-ajax.min或编辑的解决方案都web.config允许UnobtrusiveJavaScriptEnabled在我的情况下完成,但我的 ajax 表单仍然在做一个完整的回发并且不能明白为什么。

View

@using (Ajax.BeginForm("Login", "User", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "loginResult" }, new { @id = "loginForm", @name = "loginForm" }))
    {
        <span id="loginResultx">@(TempData["loginResult"] != null ? TempData["loginResult"].ToString() : "")</span>
        <span id="loginResult"></span>
        <span>
            @Html.Label("User Id")
            @Html.TextBox("id")</span>
        <span>@Html.Label("Password")
            @Html.Password("pwd")
        </span>
        <span>
            <input type="submit" value="Submit" />
        </span>
    }

Controller

public ActionResult Login(FormCollection loginForm)
    {
        if (Request.HttpMethod == "POST")
        {
            var id= loginForm["id"];
            var pwd = loginForm["pwd"];
            bool status = false;
            User obj = new User();
            status = obj.Login(id, pwd);
            if (status)
            {
                Session["id "] = id;
                return RedirectToRoute("Profile");

            }

            TempData["loginResult"] = "Login Failed";
        }
        return View();
    }

BundleConfig

bundles.Add(new ScriptBundle("~/scr").Include("~/Scripts/jquery-1.7.1.min.js", "~/Scripts/modernizr-2.5.3.js", "~/Scripts/jquery.validate.min.js", "~/Scripts/jquery.unobtrusive-ajax.min"));
4

1 回答 1

3

你确定不是这条线有问题吗?

bundles.Add(new ScriptBundle("~/scr").Include("~/Scripts/jquery-1.7.1.min.js", "~/Scripts/modernizr-2.5.3.js", "~/Scripts/jquery.validate.min.js", "~/Scripts/jquery.unobtrusive-ajax.min"));

您在 ajax 文件包含中缺少 .js。你看到了吗?

无论哪种方式,我都会使用 Chrome 检查器/Fiddler 来分析任何 JS 错误或缺少的包含。

于 2013-04-05T19:39:45.143 回答