一、非強(qiáng)類(lèi)型:
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)類(lèi)型:
DropDownListFor常用的是兩個(gè)參數(shù)的重載,第一參數(shù)是生成的select的名稱(chēng),第二個(gè)參數(shù)是數(shù)據(jù),用于將綁定數(shù)據(jù)源至DropDownListFor
Modle:
public class SettingsViewModel
{
Repository rp =new Repository();
public string ListName { get; set; }
public IEnumerableSelectListItem> 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)選擇")
您可能感興趣的文章:- Asp.net Mvc 身份驗(yàn)證、異常處理、權(quán)限驗(yàn)證(攔截器)實(shí)現(xiàn)代碼
- 使用asp.net MVC4中的Bundle遇到的問(wèn)題及解決辦法分享
- asp.net MVC實(shí)現(xiàn)無(wú)組件上傳圖片實(shí)例介紹
- ASP.NET MVC中為DropDownListFor設(shè)置選中項(xiàng)的方法
- ASP.NET MVC 5使用X.PagedList.Mvc進(jìn)行分頁(yè)教程(PagedList.Mvc)
- ASP.NET MVC3關(guān)于生成純靜態(tài)后如何不再走路由直接訪(fǎng)問(wèn)靜態(tài)頁(yè)面
- 基于A(yíng)sp.Net MVC4 Bundle捆綁壓縮技術(shù)的介紹
- ASP.NET MVC使用EasyUI的datagrid多選提交保存教程
- Asp.net MVC scheduler的實(shí)現(xiàn)方法詳解