First, create your ViewModel.
public class MovieViewModel
{
public string Genre { get; set; }
public IEnumerable<SelectListItem> GenreList
{
get
{
yield return new SelectListItem { Text = "Comedy", Value = "1" };
yield return new SelectListItem { Text = "Drama", Value = "2" };
yield return new SelectListItem { Text = "Documentary", Value = "3" };
}
}
}
Then, your controller creates a new instance of this ViewModel and sends it to the view.
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
var viewModel = new MovieViewModel
{
Genre = "2"
};
return View(viewModel);
}
}
Finally, the view displays the dropdownlist using the ASP.NET wrapper [Html.DropDownListFor()][1].
@model MvcApplication1.Models.MovieViewModel
<!DOCTYPE html>
<html>
<head>
<title>My movie</title>
</head>
<body>
<div>
@Html.DropDownListFor(m => m.Genre, Model.GenreList)
</div>
</body>
</html>
The selected value is then automatically chosen according the ViewModel. There's no need to manually set the Selected property of the objects SelectListitem.