如何使用ajax加载ValidationSummary?我试着使用MVC的现成成员资格。
简单的问题,但我被困住了。
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
[RecaptchaControlMvc.CaptchaValidator]
public ActionResult Register(RegisterModel model, bool captchaValid, string captchaErrorMessage)
{
if (ModelState.IsValid)
{
// Attempt to register the user
try
{
if (captchaValid)
{
WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
WebSecurity.Login(model.UserName, model.Password);
return RedirectToAction("Index", "Home");
}
ModelState.AddModelError("", captchaErrorMessage);
}
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
// If we got this far, something failed, redisplay form
return View(model);
}查看:
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary()
<fieldset>
<legend>Registration Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
<input type="hidden" id ="some" value=""/>
</li>etc.我不想重定向每一次,例如,如果用户名存在或等。
发布于 2013-04-03 15:40:58
为此,可以将部分视图返回为html。呈现的部分将包含模型状态错误,因此在返回为html时将显示。
示例
可以创建一个名为AjaxResult的类。
public class AjaxResult
{
public string Html { get; set; }
public bool Success { get; set; }
}然后,在ajax调用的成功函数中,可以将html附加到适当的元素。例如:
$.ajax({
url: 'http://bacon/receive',
dataType: "json",
type: "POST",
error: function () {
},
success: function (data) {
if (data.Success) {
$('body').append(data.Html);
}
}
});https://stackoverflow.com/questions/15790574
复制相似问题