In our application we want to add new movies, and select directors from a drop-down list. The classes are structured as follows.
public class Movie
{
public int Id { get; set; }
public string Title { get; set; }
public virtual Director Director { get; set; }
public virtual int DirectorId { get; set; }
}
public class Director
{
public int Id { get; set; }
public string Name { get; set; }
}
HTML.DROPDOWNLIST VS HTML.DROPDOWNLISTFOR To add the drop-down markup to your .cshtml page you could of course simply write out a select element by hand, but then you lose out on validation. MVC provides HTML helpers for generating common HTML elements. For an HTML select element you have two choices: Html.DropDownList and Html.DropDownListFor. The difference is the way they reference the name attribute of the resulting HTML element. DropDownList takes as it’s first argument a string that will be turned into the form field. So the call
@Html.DropDownList("director", directorList)
//assume directorList is an IEnumerable of SelectListItem to create options from
Will result in an html element that looks like this.
<select id="director" name="director">...</select>
The problem with this approach is if you change the name of the property on your model from ‘director’ to ‘auteur’ you won’t get compile time checking and your form will no longer work with model binding. Html.DropDownListFor was introduced in MVC 2 and allows binding to strongly typed views. The first argument should be a lambda function that returns the model property you want the control to bind to. So in our case if the view-model includes a property DirectorId we can create a drop-down list with the code
@Html.DropDownListFor(viewModel => viewModel.DirectorId, directorList)
Which generates the following html
<select id="DirectorId" name="DirectorId">
...
</select>
Now if we change the name of the Director property our build will break because the lambda expression will be invalid.Note that we’re using DirectorId instead of a Director object because we likely want to store the id in a foreign key.
POPULATING DROP-DOWN LIST OPTIONS To populate a drop-down we need to pass the HTML helper an IEnumerable. This is easily created by making a SelectList object in your controller, and passing it in via a view-model.
public ActionResult Index()
{
var directors = new Collection
{
new Director {Id = 1, Name = "David O. Russell"},
new Director {Id = 2, Name = "Steven Spielberg"},
new Director {Id = 3, Name = "Ben Affleck"}
};
var selectList = new SelectList(directors, "Id", "Name");
var vm = new ViewModel {DirectorSelectList = selectList};
return View(vm);
}
SETTING A DEFAULT VALUE OF THE DROP-DOWN var selectList = new SelectList(directors, "Id", "Name", new {id = 3});
To set the DirectorId property of the viewmodel to the value I want defaulted as follows.
var vm = new ViewModel {DirectorSelectList = selectList, DirectorId = 3}; Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif
Kishore22, if this helps please login to Mark As Answer. | Alert Moderator