• Mvc中DropDownList 和DropDownListFor的常用方法


     

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

  • 相关阅读:
    使用Dubbox构架分布式服务
    Elasticsearch搜索之explain评分分析
    Elasticsearch搜索之most_fields分析
    Elasticsearch搜索之cross_fields分析
    Elasticsearch搜索之best_fields分析
    Elasticsearch高级搜索排序( 中文+拼音+首字母+简繁转换+特殊符号过滤)
    ibatis2.3中#和$符号的区别(转)
    读文章有感
    图片bmp格式转换为jpg格式
    IndexOf、LastIndexOf、Substring的用法
  • 原文地址:https://www.cnblogs.com/lgx5/p/5284584.html
Copyright © 2020-2023  润新知