Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Required Field Indicator

For fields in my ASP.NET MVC view that have been attributed as required, is there any way for the framework to render some sort of indicator automatically that the field is marked as required in metadata?

like image 747
Benny Avatar asked Jan 10 '11 19:01

Benny


People also ask

How will you implement required field validator in MVC?

From the model class drop down, select "Employee" as the model. Click on the "Add" button. It will automatically create the view from the model. Add the "[Required]" attribute to the field that you want to make mandatory for insertion.

How do you indicate mandatory fields?

A distinctive sign (“*” symbol, “mandatory” mention, etc.) must be provided in the label of each mandatory field. If a symbol is used to declare mandatory fields, a statement placed at the beginning of the form must indicate that the symbol stands for a mandatory field.


1 Answers

Should be able to do this with CSS since MVC3 adds in those custom attributes to the element:

 <input data-val="true" data-val-required="The Username field is required." id="Username" name="Username" type="text" value="" />

You could key off the data-val-required in CSS like so:

input[data-val-required] { background:red } or set a background image of an asterisk etc.

like image 61
Francis Shanahan Avatar answered Sep 24 '22 20:09

Francis Shanahan