Binding static dropdown list with model in mvc3/mvc4 razor

In this tutorial I will show you that how can you bind your static drop down list with you view model.

@{
  var listItems = new List<ListItem> { new ListItem { Text = "Single", Value = "Single" }, new ListItem { Text = "Married", Value = "Married" }, new ListItem { Text = "Divorse", Value = "Divorse" } };
  }
  @Html.DropDownListFor(model => model.EmployeeDetail.MaritalStatus, new SelectList(listItems), "-- Select Status --")

Above code is placed in view only.
First what I did is that I made list of listitems (Here I have used example of marital status)

var listItems = new List<ListItem> { new ListItem { Text = "Single", Value = "Single" }, new ListItem { Text = "Married", Value = "Married" }, new ListItem { Text = "Divorse", Value = "Divorse" } };

After this I have just passed this list to @Html.Dropdownlistfor where model & list are bidden.

@Html.DropDownListFor(model => model.EmployeeDetail.MaritalStatus, new SelectList(listItems), "-- Select Status --")

Like this you can bind your static list with model.
If model has any default value then it will be selected in drop down & also when we will select any value in drop down then we can get this value from model in controller.

admin

  • Customermain Mail

    how to get selected value from dropdownwlist to controller ?