一、非強(qiáng)類型:
Controller:
ViewData["AreId"] = from a in rp.GetArea()
select new SelectListItem {
Text=a.AreaName,
Value=a.AreaId.ToString()
};
View:
@Html.DropDownList("AreId")
還可以給其加上一個(gè)默認(rèn)選項(xiàng):@Html.DropDownList("AreId", "請(qǐng)選擇");
二、強(qiáng)類型:
DropDownListFor常用的是兩個(gè)參數(shù)的重載,第一參數(shù)是生成的select的名稱,第二個(gè)參數(shù)是數(shù)據(jù),用于將綁定數(shù)據(jù)源至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(),"請(qǐng)選擇")
聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com