1. Non-strong type:
Controller:
Viewdata ["areid"] = from a in RP. getarea ()
Select New selectlistitem {
TEXT = A. areaname,
Value = A. areaid. tostring ()
};
View:
@ Html. dropdownlist ("areid ")
You can also add a default option: @ html. dropdownlist ("areid", "select ");
Ii. Strong type:
Dropdownlistfor is often used to overload two parameters. The first parameter is the generated select name, and the second parameter is data, used to bind the data source to dropdownlistfor
Modle:
Public class settingsviewmodel
{
Repository Rp = new repository ();
Public String listname {Get; set ;}
Public ienumerable <selectlistitem> getselectlist ()
{
VaR selectlist = RP. getarea (). Select (A => New selectlistitem {
TEXT = A. areaname,
Value = A. areaid. tostring ()
});
Return selectlist;
}
}
Controller:
Public actionresult index ()
{
Return view (New settingsviewmodel ());
}
View:
@ Model mvc3applicationtest2. Models. settingsviewmodel
@ Html. dropdownlistfor (M => M. listname, model. getselectlist (), "select ")