MVC Html.DropDownList 和DropDownListFor 的常用方法
2024-08-27 02:18:01
一、非强类型:
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(),"请选择")
最新文章
- eclipse如何添加Memory Analyzer
- 关于js中的时间处理
- eclipse 搭建python环境
- 各廠商ERP系統架構圖連結 (ERP流程圖)(轉)
- bat批处理设置Java JDK系统环境变量文件
- CSS3-给网页添加图片
- 【leetcode❤python】242. Valid Anagram
- Linux用户查询
- poj----(1470)Closest Common Ancestors(LCA)
- table注意事项
- 在Windows Server2016里通过Hyper-V安装Liunx Container
- php逐行读取txt文件写入数组的方法
- 06 Activity 4中启动模式
- 用PHPExcel导出导入Excel
- Bootstrap手风琴效果
- 二分查找、two points、排序
- eslint 代码规范2
- 【PHP】 mysqli_autocommit() 函数
- hdu2597 Simpsons’ Hidden Talents
- 编程技巧:使用异或操作符(XOR)交换两数值
热门文章
- wireshark的提示
- “App Store加载失败,使已购页面再试一次”解决方案
- DevExpress相关控件中非字符数值居左显示
- Spring读取配置文件,获取bean的几种方式
- BZOJ2933 [Poi1999]地图【区间DP】
- 使用 ExceptionDispatchInfo 捕捉并重新抛出异常
- andriod studio报错 Emulator: emulator: ERROR: x86 emulation currently requires hardware acceleration! Emulator: Process finished with exit code 1
- Promise详解
- java 并发synchronized使用
- javascript把json串转成对象