[RESOLVED]Dynamically assign Required Field Validator on a field

I have an MVC application.

I have a situation where I want to dynamically control a field in a form to be "Required".

I have a list of all the users of my website listed. To remove a user we have a link saying DELETE. When the user clicks on the link I open up a fancy box with a form. First I have to determine whether the user is a employee at my company or some other user.

If the user is an employee then I would have to select a reason from a drop down box stating why I am removing him. In that case the drop down box is mandatory. If the user is just an user then the drop down is not mandatory. How can I switch between Required
or not Required?

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "formRemoveMergeSpecifiedBrand" }))
       <span style="color: Maroon;">Reason</span><br />
       @Html.DropDownListFor(model => model.Reason, new SelectList(Model.Reasons, "ReasonId", "ReasonName"), "Please select one", new { style = "width: 250px;" })


    <div><br /></div>

    <input type="submit" value="Submit" class="button" />

Please advise.

Thank you


If you want to do the Dynamic Validation in Client Side you just set the rule like this -


But i would say creating new validation attribute is a better solution. Refer the link for the same -



If you can extent your project please use the fluent validator, if not you have to right your own custom conditional validator or use the jquery to validate your filed instead of model validation


Leave a Reply