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" })隐藏

 

 

转载于:https://www.cnblogs.com/aaronguo/archive/2013/01/15/2861274.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值