一、非强类型:

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(),"请选择")

MVC Html.DropDownList 和DropDownListFor 的常用方法的更多相关文章

  1. Mvc中DropDownList 和DropDownListFor的常用方法

    Mvc中DropDownList 和DropDownListFor的常用方法 一.非强类型: Controller:ViewData["AreId"] = from a in rp ...

  2. MVC下拉框Html.DropDownList 和DropDownListFor 的常用方法

    一.非强类型:Controller:ViewData["AreId"] = from a in Table                               select ...

  3. asp.net mvc中DropDownList

    asp.net mvc中DropDownList的使用. 下拉列表框 以分为两个部分组成:下拉列表和默认选项 DropDownList扩展方法的各个重载版本基本上都会传递到这个方法上:   publi ...

  4. MVC 中DropDownList 用法

    MVC 中DropDownList  用法 后台 Dictionary<string, int> dc = new Dictionary<string, int>(); dc. ...

  5. Asp.Net MVC绑定DropDownList等控件

    测试环境:vs2013..Net4.5.mvc5 一.Asp.Net MVC绑定控件原理说明 以Html.TextBox为例 /// <param name="name"&g ...

  6. 下拉框Html.DropDownList 和DropDownListFor 的经常用法

    一.非强类型: Controller: ViewData["AreId"] = from a in rp.GetArea()                             ...

  7. ASP.NET MVC中DropDownList的使用

    Asp.net MVC中的DropDownLists貌似会让一开始从Asp.net Forms转过来的程序员造成不少迷惑.这篇文章讲述了为了使用DropDownLists,你需要在Asp.Net MV ...

  8. Mvc HtmlHelper 方法扩展 DropDownListFor

      项目中遇到表单提交中遇到枚举,忽然想起1年前的1小段代码结合HtmlHelper在扩展一下 便于开发中使用 public static class HtmlHelperExtensions { p ...

  9. MVC @Html.DropDownList()绑定值

    Controller中: ViewBag.modules = new SelectList(集合.ToList(), "下拉框键", "下拉框值"); View ...

随机推荐

  1. iOS数组排序 请求后,数组元素的排序 时间戳,最热,点赞数等

    [ZOYSessionManager dataWithUrlString:GetVideoDataComment andParameter:@{@"id":userID,@&quo ...

  2. IOS开发 static关键字的作用

    (1)函数体内 static 变量的作用范围为该函数体,不同于 auto 变量,该变量的内存只被分配一次, 因此其值在下次调用时仍维持上次的值: (2)在模块内的 static 全局变量可以被模块内所 ...

  3. python 怎么画图

    1 安装matplotlib: 安装方法:http://www.2cto.com/os/201309/246928.html(其中,安装过程中,tar解压怎么解都有问题.然后就删掉再下载一遍) 2 使 ...

  4. java程序编写需注意的问题

    初学java,免不了很多注意事项 加分号 类名与文件名一致 javac fileName而非javac fileName.class ```java System.out.println(" ...

  5. How to input the newline in Numbers of Mac?

    newline control+enter

  6. erl_0020 《面对软件错误构建可靠的分布式系统》读书笔记001 “面向并发COPL”

    在现实世界中,顺序化的(sequential)活动非常罕见.当我们走在大街上的时候,如果只看到一件事情发生的话我们一定会感到不可思议,我们期望碰到许多同时进行的活动. 如果我们不能对同时发生的众多事件 ...

  7. 《DSP using MATLAB》示例Example 8.29

    %% ------------------------------------------------------------------------ %% Output Info about thi ...

  8. php、打印

    <!DOCTYPE HTML><html><head><meta http-equiv="content-type" content=&q ...

  9. c++深拷贝/浅拷贝

    拷贝构造函数,是一种特殊的构造函数,它由编译器调用来完成一些基于同一类的其他对象的构建及初始化.其唯一的参数(对象的引用)是不可变的(const类型).此函数经常用在函数调用时用户定义类型的值传递及返 ...

  10. 嵌入式无法使用QAudioDeviceInfo类

    修改: 1.修改pro文件 增加    QT += multimedia 2.修改Makefile文件,LIBS = $(SUBLIBS)  -L/opt/qt-4.8.5/lib -lQtMulti ...