MvcDropDownList DropDownListFor的常用方法

一、非强类型:

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中DropDownList 和DropDownListFor的常用方法的更多相关文章

  1. MVC Html.DropDownList 和DropDownListFor 的常用方法

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

  2. asp.net mvc中DropDownList

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

  3. MVC 中DropDownList 用法

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

  4. ASP.NET MVC中DropDownList的使用

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

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

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

  6. Asp.Net MVC中DropDownListFor的用法(转)

    2016.03.04 扩展:如果 view中传入的是List<T>类型 怎么使用 DropList 既然是List<T> 那么我转化成 T  List<T>的第一个 ...

  7. Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  8. 转:Asp.Net MVC中DropDownListFor的用法

    在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值.用法不复杂,这里简单做一个记录. 首先我们要定义一个 Model ,用户在 DropDownLis ...

  9. 关于mvc中@Html.DropDownListFor和@Html.DropDownList默认值无法选中问题简单总结

    当我们在做类似编辑功能的时候,会给定select选中默认值,然而mvc中偶尔这个功能不能用,或者是强类型的@Html.DropDownListFor不能用.凑巧今天遇到问题,解决问题时发现了mvc的一 ...

随机推荐

  1. ButterKnife 注解

    简介 官网:http://jakewharton.github.io/butterknife/ github:https://github.com/JakeWharton/butterknife 依赖 ...

  2. javascript 实用函数

    1.去除字符串空格 /*去左空格*/ function ltrim(s) { return s.replace(/^(\s*| *)/, ""); } /*去右空格*/ funct ...

  3. The following module was built either with optimizations enabled or witherout debug information

    出现这个问题的原因是这个程式有做版控,服务器上的版本比本机版本小 解决方式为:删除服务器上的版控或者本机版本改成与服务器一致即可

  4. leetcode修炼之路——13. Roman to Integer

    题目: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from ...

  5. sql问题

    表中某个指标重复,去掉重复项: select * from #temp where A0107 in (select A0107 from #temp  group by A0107having CO ...

  6. 【USACO 1.2.3】命名那个数字

    [问题描述] 在威斯康辛州牛大农场经营者之中,都习惯于请会计部门用连续数字给母牛打上烙印.但是,母牛用手机时并没感到这个系统的便利,它们更喜欢用它们喜欢的名字来呼叫它们的同伴,而不是用像这个的语句&q ...

  7. 9 Palindrome Number(判断是否为回文数Easy)

    题目意思:判断是否为回文数,不许使用额外空间 ps:一直不理解额外空间的意思,int能用吗 思路:1.比较头尾 2.翻转,越界问题需考虑 class Solution { public: bool i ...

  8. oracle行转列和列转行

    目录[-] 一.行转列 1.1.初始测试数据 1.2. 如果需要实现如下的查询效果图: 1.3.延伸 二.列转行 1.1.初始测试数据 1.2. 如果需要实现如下的查询效果图: 一.行转列 1.1.初 ...

  9. 由于权限不足而无法读取配置文件出现的HTTP 500.19解决办法

    无法访问请求的页面,因为该页的相关配置数据无效. 如下图: 解决方法, 到站点目录的属性,安全标签,添加用户(Everyone),并给修改权限:

  10. 【行为型】Visitor模式

    访问者模式意图在不改变目标对象类结构的情况下,扩展对象类的行为.新扩展的行为均被封装成类对象的形式来动作.应用该模式时,往往目标类系与Visitor类系是两个不同的类系,它们配合工作但维护是分开的,互 ...