RequiredIf with object client side not working.

Aug 25, 2012 at 2:38 PM

Hello everyone

I have a select object with possible Marital statuses and a text box “SpouseName” which should be required if from select “Married” selected.

I spent a several hours trying to make it work with RequiredIf attribute, but no luck.

Can anyone please let me know how to make it work?

Here is my scripts:

@Scripts.Render("~/bundles/modernizr")
 @Scripts.Render("~/Scripts/jquery-1.7.1.js")
@Scripts.Render("~/Scripts/jquery-ui-1.8.")
@Scripts.Render("~/Scripts/jquery.validate.js")
@Scripts.Render("~/Scripts/jquery.validate.unobtrusive.js")
@Scripts.Render("~/Scripts/mvcfoolproof.unobtrusive.js")

Here are my select and textbox from cshtml view:

 @Html.DropDownListFor(model => model.PatientMaritalStatus.MaritalStatusID,
n
ew SelectList(new AdultDayCare.Models.AdultDayCareContext().LookUp.Where(p => p.LookupValueCode.Equals("ms")).ToList().OrderBy(l => l.LookupValueString), "LookupValueID", "LookupValueString", 0), "-- Select Marital Status --",
new { id = "PatientMaritalStatus_MaritalStatusID" })
@Html.ValidationMessageFor(model => model.PatientMaritalStatus.MaritalStatusID, "Marital Status Required")

 

@Html.EditorFor(model => model.SpouseName)
@Html.ValidationMessageFor(model => model.SpouseName)

 And here is html output:

<div class="editor-field">

           <select data-val="true" data-val-number="The field MaritalStatusID must be a number." data-val-required="The MaritalStatusID field is required." id="PatientMaritalStatus_MaritalStatusID" name="PatientMaritalStatus.MaritalStatusID">
<
option value="">-- Select Marital Status --</option>
<option value="3">Divorced</option>
<option value="2">Married</option>
<option value="4">Separated</option>
<option value="1">Single</option>
<option value="5">Widowed</option>
</select>

           <span class="field-validation-valid" data-valmsg-for="PatientMaritalStatus.MaritalStatusID" data-valmsg-replace="false">Marital Status Required</spa
</div>

<div class="editor-label">
<label for="SpouseName">Spouse Name</labe>
</div>
<div class="editor-field">

 <input class="text-box single-line" data-val="true" data-val-requiredif="Spouse Name is required due to PatientMaritalStatus.MaritalStatusID being equal to 2" data-val-requiredif-dependentproperty="PatientMaritalStatus.MaritalStatusID" data-val-requiredif-dependentvalue="2" data-val-requiredif-operator="EqualTo" id="SpouseName" name="SpouseName" type="text" value="" />

           <span class="field-validation-valid" data-valmsg-for="SpouseName" data-valmsg-replace="true"></span>

       </div>

 Thank you very much.

Sincerely, Y.