Search code examples
model-view-controllerdrop-down-menuwebgridhtml.dropdownlistfor

HtmlDropDownList SelectList is a list of Model and how to display models Property


I have a webgrid and inside it each item has different selectList which consist of model. I want to display name of that model. How can I do that

grid.Column(header: "AvailableDevices", format: @item => Html.DropDownList("value", (IEnumerable<SelectListItem>)item.AvailableDevices)),

List<XXModel> myList = new List<XXModel>();
item.AvailableDevices = new SelectList(myList );

and XX model has property of Name, Age etc. And I want to display name or Age in my dropdown List


Solution

  • you can use this constructor of SelectList:

    item.AvailableDevices = new SelectList(myList,"Id","Name" );
    

    See all Available Contructors of SelectList