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

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM