Firdt I recommend that you create a viewmodel class for your view:
public class IndexViewModel
{
public IList<string> Roles { get; set; }
public string SelectedRole { get; set; }
}
Then call the view like this:
public ActionResult Index()
{
List<string> roles = new List<string>
{
"Developer",
"Tester",
"Project Manager",
"Team Lead",
"QA"
};
var viewModel = new IndexViewModel();
viewModel.Roles = roles;
return this.View(viewModel);
}
Then finally, render the dropdown list:
@model Mvc4.Controllers.IndexViewModel
@Html.DropDownListFor(model => model.SelectedRole, new SelectList(Model.Roles))
You needed a variable for storing the selected item (SelectedRole) and you need to wrap the selection of roles into a SelectList, since the dropdown helper cannot use an IEnumerable for the second parameter.
saisagarnallaagilelearning-15961, if this helps please login to Mark As Answer. | Alert Moderator