zoukankan      html  css  js  c++  java
  • 下拉框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(),"请选择")
  • 相关阅读:
    ThreadSafety with the AutoResetEvent, ManualResetEvent Class(Synchronization of .net)
    使用Python SMTP发送邮件
    flask项目中设置logo
    如何解决Bootstrap中分页不能居中的问题
    pip install mysql_python报错解决办法
    git上拉项目
    AttributeError: 'str' object has no attribute 'decode'
    pycharm设置SDK
    为git创建远程仓库
    开发过程中git的使用
  • 原文地址:https://www.cnblogs.com/mfrbuaa/p/4078834.html
Copyright © 2011-2022 走看看