insert drop down in mvc grid 4.0 razor

Posted by saisagarnallaagilelearning-15961 under jQuery on 6/26/2013 | Points: 10 | Views : 1704 | Status : [Member] | Replies : 1
how to insert Dropdown box into a mvc razor grid view ?
i am using only palin visual studio tools( not like telericks and devexpress like)..


Posted by: Allemahesh on: 6/27/2013 [Member] [MVP] Silver | Points: 25

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>
"Project Manager",
"Team Lead",

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

Login to post response