In MVC3 data annotations can be used to speed up the UI development and validations; ie.
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "New password")]
public string NewPassword { get; set; }
However, if for a mobile app, there is no field label, only a drop-down list populated from the database. How would I define this in this way?
[Required]
[DataType(DataType.[SOME LIST TYPE???])]
[Display(Name = "")]
public string Continent { get; set; }
Is it better not to use this method for this?
Change your ViewModel like this
and in your
GET
Action method, Set the Get the Data from your DB and set the Continents Collection property of your ViewModeland in your
View
(DoThatStep.cshtml
) use thisNow this will make your DropDown Required field.
If you want to enforce the selection of an element in the DropDown use the
[Required]
attribute on the field you are binding to:and in your view: