zoukankan      html  css  js  c++  java
  • Html.RadioButtonFor和Html.DropDownListFor 用法备忘

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

    Html.RadioButton和二、强类型:用法

    强类型:

    Controller

      public ActionResult Index()
           {
               return View(new SettingsViewModel());
           }

    View

    @Html.RadioButtonFor(Model => Model.Status, 0, new { @id = "Statusradio0", @name = "Status" })正常

    @Html.RadioButtonFor(Model => Model.Status, 1, new { @id = "Statusradio1", @name = "Status" })冻结

    @Html.RadioButtonFor(Model => Model.Status, 2, new { @id = "Statusradio2", @name = "Status" })隐藏

  • 相关阅读:
    jsp 特殊标签
    poj 1753 Flip Game 高斯消元 异或方程组 求最值
    zoj 3155 Street Lamp 高斯消元 异或方程组 求方案数
    poj1222 EXTENDED LIGHTS OUT 高斯消元解异或方程组 模板
    zoj 3930 Dice Notation 模拟
    zoj 3157 Weapon 线段树求逆序对数
    hdu 1242 Rescue BFS+优先队列
    hdu 3466 Proud Merchants 贪心+01背包
    zoj 3689 Digging 贪心+01背包
    hdu 2602 Bone Collector 01背包模板
  • 原文地址:https://www.cnblogs.com/aaronguo/p/2861274.html
Copyright © 2011-2022 走看看