我需要一些关于使用 ajax 进行验证的帮助。我的登录对话框有效,但它不处理成功或存在验证错误时发生的情况。我不确定我如何或在哪里实际创建 ajax 帖子(我对 jquery/Ajax 没有经验)
这是我的观点@model Models.LoginModel
@{
ViewBag.Title = "Log in";
}
<hgroup class="title">
@* <h1>@ViewBag.Title.</h1>*@
</hgroup>
<section id="loginForm" style="margin-left: 20px; border-width: 0px;">
@using (Ajax.BeginForm("Login", new { ReturnUrl = ViewBag.ReturnUrl }, new AjaxOptions { HttpMethod = "Post" }, new { Id = "login_form" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
</fieldset>
@*<p>
@Html.ActionLink("Register", "Register") if you don't have an account.
</p>*@
}
</section>
</div>
<script type="text/javascript">
$(document).ready(function () {
$("#dlgLogin").dialog({
modal: true,
autoOpen: true,
draggable: true,
resizable: true,
title: "Login",
buttons: {
Login: function () {
$("#login_form").submit();
},
Cancel: function () {
$(this).dialog("close");
}
}
});
});
</script>
这是我的控制器中的 loginpost 方法。
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
//return RedirectToLocal(returnUrl);
return Json(new { success = true });
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return PartialView("LoginDialog",model);
}