我曾尝试使用Requiredif和RequiredifTrue,但两者都无效。下面是我的代码。当我将IsmedicalInsurance的值设为true / false时,其他两个都无法正常工作。
[Required(ErrorMessage="Please update about medical insurance")]
public bool? IsMedicalInsurance { set; get; }
[RequiredIf("IsMedicalInsurance", true, ErrorMessage = "Enter Primary Insuracne Name")]
public string PrimaryInsurance { set; get; }
[RequiredIfTrue("IsMedicalInsurance", ErrorMessage = "Enter Id number")]
public string IDnumber { set; get; }
查看代码。
<label class="radio-inline">
@Html.RadioButtonFor(m => m.IsMedicalInsurance, true, new { id="rdhaveinsur"}) Yes
</label>
<label class="radio-inline">
@Html.RadioButtonFor(m => m.IsMedicalInsurance, false, new { id="rdhaveno"})No
@Html.ValidationMessageFor(m => m.IsMedicalInsurance)
</label>
<div class="col-sm-7">
@Html.TextBoxFor(m => m.PrimaryInsurance, new { @class = "form-control", id = "txtInsurance" })
@Html.ValidationMessageFor(m => m.PrimaryInsurance)
</div>
<div class="col-sm-7">
@Html.TextBoxFor(m => m.IDnumber, new { @class = "form-control", id = "txtIdNumber" })
@Html.ValidationMessageFor(m => m.IDnumber)
</div>
答案 0 :(得分:0)
为什么你需要让IsMedicalInsurance可以为空,因为该值为true,不应为null?我已经尝试了你的代码,它正在使用IsMedicalInsurance更改为bool而不是bool
<强>更新强>
你是否在脚本下面加载了?
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/jqueryval")
@Scripts.Render("~/Scripts/mvcfoolproof.unobtrusive.min.js")