Asp.net mvc ASP.NETMVC:用户必须在客户端选中一个复选框进行验证
我有一个UI,其中显示了3个复选框,每个复选框都引用了模型类的不同属性。我只是通过mvc数据注释使用jquery进行不引人注目的验证。我想当用户提交表单时,用户必须选中一个复选框,否则将显示客户端错误消息,表单将不会提交 我可以通过jquery实现,但我想通过mvc数据注释实现 看我的模型课 控制器 通过下面的代码,我可以验证是否从服务器端代码中选择了任何复选框,并添加在客户端显示错误的模型错误 但我想通过客户端使用普通数据注释进行验证 查看我的剃须刀代码Asp.net mvc ASP.NETMVC:用户必须在客户端选中一个复选框进行验证,asp.net-mvc,asp.net-validators,Asp.net Mvc,Asp.net Validators,我有一个UI,其中显示了3个复选框,每个复选框都引用了模型类的不同属性。我只是通过mvc数据注释使用jquery进行不引人注目的验证。我想当用户提交表单时,用户必须选中一个复选框,否则将显示客户端错误消息,表单将不会提交 我可以通过jquery实现,但我想通过mvc数据注释实现 看我的模型课 控制器 通过下面的代码,我可以验证是否从服务器端代码中选择了任何复选框,并添加在客户端显示错误的模型错误 但我想通过客户端使用普通数据注释进行验证 查看我的剃须刀代码 @使用(Html.BeginForm
@使用(Html.BeginForm(“Index”,“Customers”,FormMethod.Post,new{@class=“form horizontal”,role=“form”}))
{
@Html.AntiForgeryToken()
输入客户信息。
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@LabelFor(m=>m.FirstName,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.FirstName,新的{@class=“form control”})
@Html.ValidationMessageFor(m=>m.FirstName,“,new{@class=“text danger”})
@LabelFor(m=>m.LastName,新的{@class=“col-md-2控制标签”})
@TextBoxFor(m=>m.LastName,新的{@class=“form control”})
@Html.ValidationMessageFor(m=>m.LastName,“,new{@class=“text danger”})
@CheckBoxFor(m=>m.SelfSend)
@LabelFor(m=>m.SelfSend)
@Html.CheckBoxFor(m=>m.thirdParty)
@Html.LabelFor(m=>m.thirdParty)
@CheckBoxFor(m=>m.Others)
@LabelFor(m=>m.Others)
@ValidationMessage(“Error”,“”,new{@class=“text danger”})
}
@节脚本{
@Scripts.Render(“~/bundles/jqueryval”)
}
您可以尝试编写客户模型验证属性
在三个验证属性之一中添加CheckBoxAuthAttribute
在中有一个方法受保护的虚拟验证结果有效(对象值,ValidationContext ValidationContext)
,您可以在中重写
公共类CheckBoxAuthAttribute:ValidationAttribute
{
公共CheckBoxAuthAttribute(参数字符串[]propertyNames)
{
this.PropertyNames=PropertyNames;
}
公共字符串[]属性名称{get;private set;}
受保护的重写ValidationResult有效(对象值,ValidationContext ValidationContext)
{
var properties=this.PropertyNames.Select(validationContext.ObjectType.GetProperty);
var值=属性
.Select(p=>p.GetValue(validationContext.ObjectInstance,null))
.of type();
if(values.Contains(true)| |(bool)value==true)
{
返回null;
}
返回新的ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
}
公共类客户
{
[必需]
[显示(Name=“First Name”)]
公共字符串名{get;set;}
[必需]
[显示(Name=“Last Name”)]
公共字符串LastName{get;set;}
[显示(Name=“邮件给我”)]
[CheckBoxAuth(“第三方”、“其他”,ErrorMessage=“必须选择一个选项”)]
公共bool SelfSend{get;set;}
[显示(名称=“第三方”)]
公共布尔第三方{get;set;}
[显示(Name=“其他”)]
公共bool其他{get;set;}
}
您可以尝试编写客户模型验证属性
在三个验证属性之一中添加CheckBoxAuthAttribute
在中有一个方法受保护的虚拟验证结果有效(对象值,ValidationContext ValidationContext)
,您可以在中重写
公共类CheckBoxAuthAttribute:ValidationAttribute
{
公共CheckBoxAuthAttribute(参数字符串[]propertyNames)
{
this.PropertyNames=PropertyNames;
}
公共字符串[]属性名称{get;private set;}
受保护的重写ValidationResult有效(对象值,ValidationContext ValidationContext)
{
var properties=this.PropertyNames.Select(validationContext.ObjectType.GetProperty);
var值=属性
.Select(p=>p.GetValue(validationContext.ObjectInstance,null))
.of type();
if(values.Contains(true)| |(bool)value==true)
{
返回null;
}
返回新的ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
}
公共类客户
{
[必需]
[显示(Name=“First Name”)]
公共字符串名{get;set;}
[必需]
[显示(Name=“Last Name”)]
公共字符串LastName{get;set;}
[显示(Name=“邮件给我”)]
[CheckBoxAuth(“第三方”、“其他”,ErrorMessage=“必须选择一个选项”)]
公共bool SelfSend{get;set;}
[显示(名称=“第三方”)]
公共学校第三方{get
public class Customer
{
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Mail to me")]
public bool SelfSend { get; set; }
[Display(Name = "3rd party")]
public bool thirdParty { get; set; }
[Display(Name = "Others")]
public bool Others { get; set; }
}
[ValidateAntiForgeryToken()]
[HttpPost]
public ActionResult Index(Customer customer)
{
if (customer.Others == false || customer.SelfSend == false || customer.thirdParty == false)
ModelState.AddModelError("Error", "Must select one option");
return View();
}
<div class="row">
<div class="col-md-8">
<section id="testform">
@using (Html.BeginForm("Index", "Customers", FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
{
@Html.AntiForgeryToken()
<h4>Enter customer info.</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.FirstName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(m => m.LastName, new { @class = "col-md-2 control-label" })
<div class="col-md-10">
@Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.LastName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
@Html.CheckBoxFor(m => m.SelfSend)
@Html.LabelFor(m => m.SelfSend)
</div>
</div>
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
@Html.CheckBoxFor(m => m.thirdParty)
@Html.LabelFor(m => m.thirdParty)
</div>
</div>
<div class="col-md-offset-2 col-md-10">
<div class="checkbox">
@Html.CheckBoxFor(m => m.Others)
@Html.LabelFor(m => m.Others)
</div>
</div>
<div class="col-md-offset-2 col-md-10">
@Html.ValidationMessage("Error", "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
}
</section>
</div>
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
public class CheckBoxAuthAttribute : ValidationAttribute
{
public CheckBoxAuthAttribute(params string[] propertyNames)
{
this.PropertyNames = propertyNames;
}
public string[] PropertyNames { get; private set; }
protected override ValidationResult IsValid(object value, ValidationContext validationContext)
{
var properties = this.PropertyNames.Select(validationContext.ObjectType.GetProperty);
var values = properties
.Select(p => p.GetValue(validationContext.ObjectInstance, null))
.OfType<bool>();
if (values.Contains(true) || (bool)value == true)
{
return null;
}
return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));
}
}
public class Customer
{
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Mail to me")]
[CheckBoxAuth("thirdParty", "Others", ErrorMessage = "Must select one option"))]
public bool SelfSend { get; set; }
[Display(Name = "3rd party")]
public bool thirdParty { get; set; }
[Display(Name = "Others")]
public bool Others { get; set; }
}
public class CustomerVM
{
[Required]
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Required]
public int? Mailing { get; set; } // see notes below
}
@model CustomerVM
....
@using (Html.BeginForm())
{
....
<label>
@Html.RadioButtonFor(m => m.Mailing, 1, new { id = ""})
<span>Mail to me</span>
</label>
<label>
@Html.RadioButtonFor(m => m.Mailing, 2, new { id = ""})
<span>3rd party</span>
</label>
.... // ditto for "Others"
@Html.ValidationMessageFor(m => m.Mailing)
....
}
[HttpPost]
public ActionResult Index(CustomerVM model)
{
if(!ModelState.IsValid)
{
return View(model);
}
.... // map to instance of data model, save and redirect
}
public enum Mailing
{
[Display(Name = "Mail to me")]
SelfSend = 1,
[Display(Name = "3rd party")]
ThirdParty = 2,
[Display(Name = "Others")]
Others = 3
}
public class CustomerVM
{
....
[Required]
public Mailing? Mailing { get; set; }
}
@Html.RadioButtonFor(m => m.Mailing, Mailing.SelfSend, new { id = ""})