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\", \"Account\", \"\", 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\", \"Account\", 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, JsonRequestBehavior.AllowGet) :
Json(string.Format(\"{0} is not available.\", Email),
JsonRequestBehavior.AllowGet);
}
}
我的问题是,远程验证确实会触发,但没有按应有的方式向错误消息中写入任何内容,此外,jQuery方法.valid()
一直告诉我该表单有效:
(来源:balexandre.com)
我在这里想念什么?
MSDN文章显示了相同的代码(在可下载文件中)
没有找到相关结果
已邀请:
2 个回复
缮记箔
控制器:
视图:
磁辫覆氓