The example in this article describes the method of using Html.dropdownlistfor in asp.net MVC4. Share to everyone for your reference, specific as follows:
One, the controller part:
Public ActionResult Pagedetail ()
{
var thislist = _sysdepartmentbll.getalldepartmentlist ();//Data source
// Add a default data
var resultlist = new list<selectlistitem>
{
new SelectListItem {Text = "--Please select-", Selected = t Rue, Value = ""}
};
Adds a data source to the Resultlist collection
resultlist.addrange (thislist.select thismodel => new SelectListItem
{
Text = Thismodel.departmentname,
Selected = False,
Value = Thismodel.departmentid
});
Viewbag.departmentlist= getdepartmentselectlist (_sysdepartmentbll.getalldepartmentlist ());
return View ();
}
Second, pagedetail.cshtml part
Copy Code code as follows:
@Html. Dropdownlistfor (M => M.departmentid, viewbag.departmentlist as Ienumerable<selectlistitem>, new {id = " Ddldepartment "})
Or:
Copy Code code as follows:
@Html. Dropdownlistfor (M => M.departmentid, (list<selectlistitem>) viewbag.departmentlist, new {id = " Ddldepartment "})
More interested in asp.net related content readers can view the site topics: "ASP.net optimization skills summary", "asp.net string operation skills Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", "asp.net Ajax Skills Summary topic and the ASP.net caching operation skills Summary.
I hope this article will help you to ASP.net program design.