• 下拉框Html.DropDownList 和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(),"请选择")
  • 相关阅读:
    Linux下安装firefox最新版
    php开发网站编码统一问题
    WordPress前台后台页面打开慢的解决方法
    超链接标签简单的几个样式属性
    jQuery结合Ajax实现简单的前端验证和服务端查询
    Javascript配合jQuery实现流畅的前端验证
    Code-Validator:验证只包含英文字母
    Code-Validator:验证小数
    Code-Validator:验证正整数
    Code-Validator:验证非负整数
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4207758.html
Copyright © 2020-2023  润新知