My table in db have fields : Id
,Organisation
,Info
.
I want make dropdowm list like that:
You would need to create a SelectList in controller action using one of the constructors available and in view just pass it DropDownList method as parameter.
In Controller do this:
ViewBag.Organisations = new SelectList(db.Organisations.ToList(),"Id","Organisation");
in SelectList
we need to specify which property to use as value
and which to use as text
in the option
tag which we are specifying in last two parameters.
and then in View you would need to use it this way:
@Html.DropDownList("Organization",ViewBag.Organisations as SelectList)
Here first parameter would be used as name of select
element and second one will be used to populate the option
elements in the select
Following is the list of overloads available for Html.DropDownList
:
https://msdn.microsoft.com/en-us/library/system.web.webpages.html.htmlhelper.dropdownlist%28v=vs.111%29.aspx?f=255&MSPPError=-2147217396