In my Model I have this:
[RequiredIf("Operation", 2)]
public string Test_Type { get; set; }
I am using MVC Fool Proof Validation.
In my view:
<div class="col-md-6 form-group">
<label class="col-md-4 control-label">Operation Type: <span class="fa fa-asterisk text-danger"></span></label>
<div class="col-md-8">
@Html.DropDownListFor(model => model.Operation, (IEnumerable<SelectListItem>)ViewBag.OperationChoice, "-- Select Operation --", new { @class = "form-control" })
<br />
@Html.ValidationMessageFor(model => model.Operation, "", new { @class = "text-danger" })
</div>
</div>
<div class="row" id="TestType-div">
<div class="col-md-6 form-group">
<label class="col-md-4 control-label">Type Of Test: <span class="fa fa-asterisk text-danger"></span></label>
<div class="col-md-8">
@Html.DropDownListFor(model => model.Test_Type, (IEnumerable<SelectListItem>)ViewBag.Test_TypeChoice, new {@class = "form-control"})
<br/>
@Html.ValidationMessageFor(model => model.Test_Type, "", new { @class = "text-danger" })
</div>
</div>
</div>
This validation is working, but it does not highlight the Test_Type
dropdownlist in a red outline with the css class input-validation-error
. How do I use the RequiredIf
annotation in a way that when the form is invalid, it highlights the field with the input-validation-error
class name?
Check This Code Online Codepen.io
To enable client validation, include MvcFoolproofValidation.js with the standard client validation script files:
jQuery Validation If you are using jQuery validation, include MvcFoolproofJQueryValidation.js with the standard client validation script files:
MVC 3 jQuery Unobtrusive Support
If still client side validation not showing check that your drop-down has
input-validation-error
class. if class is present and not showing red border then add style :