一、非强类型:

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. Extjs 5 可选择日期+时间的组件DateTimeField

    我们都知道ExtJs有日期组件DateField,但直到ExtJs 5.0版本该日期组件也只能选择日期,不能选择时间(具体到时.分.秒),而实际工作中又常常会有需要日期和时间同时选择的需求,我们只能自 ...

  2. UNIX 基础知识

    登陆       1.登录名            系统在其 口令文件(通常是/etc/passwd文件) 中查看用户名,口令文件中包含了有关用户的信息.       2.shell          ...

  3. js效果之回到顶部

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  4. [javascript]复制到剪切板

    <!-- 一个简单的小栗子 --> <button class="copy-link" data-fulllink="要被复制的内容在这里-" ...

  5. hiredis处理zscan和hscan的reply

    zscan的返回值可以看做是一个二维数组,第一维包含两个元素:string类型的游标cursor和集合元素数组:第二维即集合元素数组,这个数组交替存放着集合元素和score,元素和score也都是st ...

  6. spring @Autowired注入的原理

    只知道如何用Autowired注解,知道可以替代set,get方法,很方便,却一直不知道,为什么可以代替 今天探索一下原因,所谓知其然还要知其所以然,才能理解的更好,记忆的更牢,才能转化为自己的知识. ...

  7. 简单实现Jmail发送邮件

    package com.chauvet.util; import java.util.Properties; import javax.mail.*; import javax.mail.intern ...

  8. VS2013中添加现有窗体项

    假如要从另一项目中拷贝窗体到本项目中,例如FmMain窗体, 需要. 把FmMain.cs 和 FmMain.Designer.cs 和 FmMain .resx 三个文件复制到程序目录下, 在vs里 ...

  9. kong k8s 安装 以及可视化管理界面

    1. git  clone $ git clone git@github.com:Mashape/kong-dist-kubernetes.git $ cd kong-dist-kubernetes ...

  10. undefined vs. null

    undefined vs. null 一.相似性 在JavaScript中,将一个变量赋值为undefined或null,老实说,几乎没区别. var a = undefined; var a = n ...