我长期以来一直对 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"));