一、非强类型:

Controller:

ViewData["AreId"] = from a in rp.GetArea()
select new SelectListItem {
Text=a.AreaName,
Value=a.AreaId.ToString()
};
View:
@Html.DropDownList("AreId")

还可以给其加上一个默认选项:@Html.DropDownList("AreId", "请选择");

二、强类型:

DropDownListFor常用的是两个参数的重载,第一参数是生成的select的名称(属性)【给属性绑定值】,第二个参数是数据,用于将绑定数据源至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(),"请选择")

最新文章

  1. eclipse如何添加Memory Analyzer
  2. 关于js中的时间处理
  3. eclipse 搭建python环境
  4. 各廠商ERP系統架構圖連結 (ERP流程圖)(轉)
  5. bat批处理设置Java JDK系统环境变量文件
  6. CSS3-给网页添加图片
  7. 【leetcode❤python】242. Valid Anagram
  8. Linux用户查询
  9. poj----(1470)Closest Common Ancestors(LCA)
  10. table注意事项
  11. 在Windows Server2016里通过Hyper-V安装Liunx Container
  12. php逐行读取txt文件写入数组的方法
  13. 06 Activity 4中启动模式
  14. 用PHPExcel导出导入Excel
  15. Bootstrap手风琴效果
  16. 二分查找、two points、排序
  17. eslint 代码规范2
  18. 【PHP】 mysqli_autocommit() 函数
  19. hdu2597 Simpsons’ Hidden Talents
  20. 编程技巧:使用异或操作符(XOR)交换两数值

热门文章

  1. wireshark的提示
  2. “App Store加载失败,使已购页面再试一次”解决方案
  3. DevExpress相关控件中非字符数值居左显示
  4. Spring读取配置文件,获取bean的几种方式
  5. BZOJ2933 [Poi1999]地图【区间DP】
  6. 使用 ExceptionDispatchInfo 捕捉并重新抛出异常
  7. andriod studio报错 Emulator: emulator: ERROR: x86 emulation currently requires hardware acceleration! Emulator: Process finished with exit code 1
  8. Promise详解
  9. java 并发synchronized使用
  10. javascript把json串转成对象