我看了this SO post并按照第一个答案做了,我做了同样的事情,但我仍然无法看到错误出现。
该模型的相关部分是:
[Display(Name = "Verify13", ResourceType = typeof(Resources.Resource))]
public bool Is13 { get; set; }
控制器是:
if (registerModel.Is13 == false)
{
ModelState.AddModelError("REGISTER_0", "You must be 13 of age or older to receive email updates.");
return View(registerModel);
}
视图的一部分是:
@Html.ValidationSummary(true)
...
@Html.LabelFor(model => model.Is13)
@Html.CheckBoxFor(model => model.Is13)
@Html.ValidationMessage("REGISTER_O")
我做错了什么?模型的其余部分在字段上具有属性。整个模型如下所示。错误消息(字段是必需的)。除此之外,每个错误都会显示出来。
public class RegisterViewModel
{
[Display(Name = "FirstName", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "FirstNameRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "FirstNameLong")]
public string FirstName { get; set; }
[Display(Name = "LastName", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "LastNameRequired")]
[StringLength(50, ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "LastNameLong")]
public string LastName { get; set; }
[Display(Name = "Email", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "EmailRequired")]
[RegularExpression(".+@.+\\..+", ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "EmailInvalid")]
public string Email { get; set; }
[Display(Name = "ConfirmEmail", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "ConfirmEmailRequired")]
[RegularExpression(".+@.+\\..+", ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "EmailInvalid")]
public string ConfirmEmailAddress { get; set; }
[Display(Name = "Password", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "PasswordRequired")]
public string Password { get; set; }
[Display(Name = "ConfirmPassword", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "ConfirmPasswordRequired")]
public string ConfirmPassword { get; set; }
[Display(Name = "ZipCode", ResourceType = typeof(Resources.Resource))]
[Required(ErrorMessageResourceType = typeof(Resources.Resource),
ErrorMessageResourceName = "ZipCodeRequired")]
public string ZipCode { get; set; }
public string[] VideoProvider { get; set; }
public IEnumerable<SelectListItem> Items { get; set; }
[Display(Name = "Verify13", ResourceType = typeof(Resources.Resource))]
public bool Is13 { get; set; }
答案 0 :(得分:1)
或者你可以装饰你的财产。
private bool isTrue
{ get { return true; } }
[Required]
[Display(Name = "I agree to the terms and conditions")]
[Compare("isTrue", ErrorMessage = "Please agree to Terms and Conditions")]
public bool iAgree { get; set; }
这是@ fields.cage回答here
通过这种方式,您可以获得应用的验证逻辑,并且您可以像其他人一样绑定它。
修改强>
在提供输出的代码下面,我想你想看到。为简单起见,我只实现了firstname和is13字段。
型号:
public class RegisterViewModel
{
[Display(Name = "FirstName")]
[Required]
public string FirstName { get; set; }
public bool isTrue
{ get { return true; } }
[Display(Name = "Verify13")]
[Required]
[System.Web.Mvc.CompareAttribute("isTrue", ErrorMessage = "You must be 13 of age or older to receive email updates.")]
public bool Is13 { get; set; }
}
查看:
@using (Html.BeginForm("Index", "Home"))
{
<table>
<tr>
<td>@Html.LabelFor(s => s.FirstName)</td>
<td>@Html.TextBoxFor(s => s.FirstName)</td>
<td>@Html.ValidationMessageFor(s => s.FirstName)</td>
</tr>
<tr>
<td>@Html.LabelFor(s => s.Is13)</td>
<td>@Html.CheckBoxFor(s => s.Is13)</td>
<td>@Html.ValidationMessageFor(s => s.Is13)</td>
</tr>
</table>
<button type="submit">Send</button>
}
控制器:
[HttpPost]
public ActionResult Index(RegisterViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
return View(model);
}
答案 1 :(得分:0)
您是否将这2个javascript文件添加到视图中(用于客户端验证):
~/Scripts/jquery.validate.min.js
~/Scripts/jquery.validate.unobtrusive.min.js
如果你使用的是Ajax.BeginForm()函数,你还需要这个文件:
~/Scripts/jquery.unobtrusive-ajax.min.js