Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use data annotations for drop-down lists?

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?

like image 888
ElHaix Avatar asked Aug 17 '12 19:08

ElHaix


People also ask

How do you use data annotations?

Data annotations (available as part of the System. ComponentModel. DataAnnotations namespace) are attributes that can be applied to classes or class members to specify the relationship between classes, describe how the data is to be displayed in the UI, and specify validation rules.

How do you show calendar with data annotation?

To create the View , right click on view folder and then click Add view. Now specify the view name as Index or as you wish, choose create scaffolding template and model class EmpModel. cs and click on Add button.


2 Answers

Change your ViewModel like this

public class RegisterViewModel
{
   //Other Properties

   [Required]
   [Display(Name = "Continent")]
   public string SelectedContinent { set; get; }
   public IEnumerable<SelectListItem> Continents{ set; get; }

}

and in your GET Action method, Set the Get the Data from your DB and set the Continents Collection property of your ViewModel

public ActionResult DoThatStep()
{
  var vm=new RegisterViewModel();
  //The below code is hardcoded for demo. you may replace with DB data.
  vm.Continents= new[]
  {
    new SelectListItem { Value = "1", Text = "Prodcer A" },
    new SelectListItem { Value = "2", Text = "Prodcer B" },
    new SelectListItem { Value = "3", Text = "Prodcer C" }
  }; 
  return View(vm);
}

and in your View (DoThatStep.cshtml) use this

@model RegisterViewModel
@using(Html.BeginForm())
{
  @Html.ValidationSummary()

  @Html.DropDownListFor(m => m.SelectedContinent, 
               new SelectList(Model.Continents, "Value", "Text"), "Select")

   <input type="submit" />
}

Now this will make your DropDown Required field.

like image 71
Shyju Avatar answered Sep 22 '22 05:09

Shyju


If you want to enforce the selection of an element in the DropDown use the [Required] attribute on the field you are binding to:

public class MyViewModel
{
    [Required]
    [Display(Name = "")]
    public string Continent { get; set; }

    public IEnumerable<SelectListItem> Continents { get; set; }
}

and in your view:

@Html.DropDownListFor(
    x => x.Continent, 
    Model.Continents, 
    "-- Select a continent --"
)
@Html.ValidationMessageFor(x => x.Continent)
like image 20
Darin Dimitrov Avatar answered Sep 23 '22 05:09

Darin Dimitrov