如何解决ASP.NET MVC3远程验证不会输出错误消息
| 我有一个简单的模型,默认为RegisterModel
,在基于该模型为Create
创建View时,我最终得到了
public class RegisterModel
{
[required]
[Remote(\"UserNameExists\",\"Account\",\"\",ErrorMessage = \"Username is already taken.\")]
[display(Name = \"Username (spaces will be stripped,must be at least 6 characters long)\")]
public string UserName { get; set; }
[required]
[Editable(true)]
[display(Name = \"First and Last name\")]
public string Name { get; set; }
[required]
[DataType(DataType.EmailAddress,ErrorMessage = \"You need to enter a valid email\")]
[Remote(\"EmailExists\",ErrorMessage = \"Email is already taken.\")]
[display(Name = \"Email address\")]
public string Email { get; set; }
//[required]
//[ValidatePasswordLength]
[DataType(DataType.Password)]
[display(Name = \"Create a password\")]
public string Password { get; set; }
[DataType(DataType.Password)]
[display(Name = \"Verify password\")]
//[Compare(\"Password\",ErrorMessage = \"Password\'s do not match.\")]
public string ConfirmPassword { get; set; }
}
并在视图中:
<h3>
Details</h3>
@using (Html.BeginForm(\"GenerateBetaLink\",FormMethod.Post,new { @id = \"beta-user\" }))
{
@Html.ValidationSummary(true)
<div>
<div class=\"editor-label\">
@Html.LabelFor(model => model.UserName)
</div>
<div class=\"editor-field\">
@Html.EditorFor(model => model.UserName)
@Html.ValidationMessageFor(model => model.UserName)
</div>
<div class=\"editor-label\">
@Html.LabelFor(model => model.Name)
</div>
<div class=\"editor-field\">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
<div class=\"editor-label\">
@Html.LabelFor(model => model.Email)
</div>
<div class=\"editor-field\">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<p>
</p>
<p>
<input type=\"submit\" value=\"Create Beta User\" class=\"btn-submit\" />
<span class=\"loading\"></span>
</p>
</div>
}
我的Validation Controller
public class ValidationController : Controller
{
public JsonResult UserNameExists(string UserName)
{
OnlineservicesRepository db = new OnlineservicesRepository();
var user = db.FindUserByUsername(UserName.Trim());
return user == null ?
Json(true,JsonRequestBehavior.AllowGet) :
Json(string.Format(\"{0} is not available.\",UserName),JsonRequestBehavior.AllowGet);
}
public JsonResult EmailExists(string Email)
{
OnlineservicesRepository db = new OnlineservicesRepository();
var user = db.FindUserByEmail(Email.Trim());
return user != null ?
Json(true,Email),JsonRequestBehavior.AllowGet);
}
}
我的问题是,远程验证确实会触发,但没有按应有的方式向错误消息中写入任何内容,此外,jQuery方法.valid()
一直告诉我该表单有效:
(来源:balexandre.com)
我在这里想念什么?
MSDN文章显示了相同的代码(在可下载文件中)解决方法
以下对我来说很好:
模型:
public class RegisterModel
{
[Required]
[DataType(DataType.EmailAddress,ErrorMessage = \"You need to enter a valid email\")]
[Remote(\"EmailExists\",\"Home\",\"\")]
[Display(Name = \"Email address\")]
public string Email { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(RegisterModel model)
{
return View(model);
}
public ActionResult EmailExists(string email)
{
if ((email ?? string.Empty).Contains(\"foo\"))
{
return Json(email + \" is not available\",JsonRequestBehavior.AllowGet);
}
return Json(true,JsonRequestBehavior.AllowGet);
}
}
视图:
@model RegisterModel
<script src=\"@Url.Content(\"~/Scripts/jquery.validate.js\")\" type=\"text/javascript\"></script>
<script src=\"@Url.Content(\"~/Scripts/jquery.validate.unobtrusive.js\")\" type=\"text/javascript\"></script>
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.Email)
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
<input type=\"submit\" value=\"OK\" />
}
,我遇到了同样的问题,并通过更新到最新的jQuery(1.6)和jQuery.validate(1.8)库解决了它。获得这些的最简单方法是在NuGet中搜索jQuery。版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。