[英]Data Annotations Checkbox
有没有一种方法可以使用C#MVC中的“数据注释”来验证布尔复选框?
我在自定义数据注释方法中看到的所有示例都只是在验证一个复选框,例如“接受条款”框。 我需要验证是否在列表<>中至少选中了一个复选框
例如:
public class QuestionOptionViewModel
{
public int? Id { get; set; }
public string Text { get; set; }
public string QuestionType { get; set; }
[RequiredIf("QuestionType", "text", ErrorMessage = "Required Field")]
public string Value { get; set; }
[RequiredIf("QuestionType", "checkbox", ErrorMessage = "Required Field")]
public bool IsChecked { get; set; }
}
我正在存储IsChecked的列表。 我想知道,使用数据注释选择了列表中的复选框之一。
视图模型
public class VisitorAgreementTermViewModel
{
[Required]
[Display(Name = "Message Accept.")]
//[Range(typeof(bool), "true", "true", ErrorMessage = "Error Message Text.")]
[RegularExpression("True", ErrorMessage = "Error Message Text.")]
public bool Agreement { get; set; }
}
视图
@using (Ajax.BeginForm("AgreementTerms", "Visitors", new AjaxOptions()
{
HttpMethod = "POST",
UpdateTargetId = "document-body",
InsertionMode = InsertionMode.Replace,
LoadingElementId = "document-loading",
OnBegin = "ClearBody('document-body')"
}))
{
@Html.AntiForgeryToken()
<div class="row">
<div class="form-group m-b-xl col-md-offset-1">
<label class="checkbox-inline">
@Html.CheckBoxFor(model => model.Agreement)
@Html.LabelFor(model => model.Agreement)
<br />
@Html.ValidationMessageFor(model => model.Agreement, "", new { @class = "text-danger" })
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
</label>
</div>
<div class="col-sm-2 col-sm-offset-9">
<button type="submit" class="btn btn-success"><span class="fa fa-fw fa-arrow-right"></span> Continue</button>
</div>
</div>
}
调节器
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AgreementTerms(AgreementTermViewModel aterms)
{
if (ModelState.IsValid)
return PartialView("_PreRegistration");
return PartialView("_AgreementTerms", aterms);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.