2016.03.04
扩展:如果 view中传入的是List<T>类型 怎么使用 DropList
既然是List<T> 那么我转化成 T List<T>的第一个,最后一个不就是M吗?
@Html.DropDownListFor(model=>model.First().Title, ViewData["Title"] as List<SelectListItem>, "标题", @"dropdownlist") </p>
在Asp.Net MVC中可以用DropDownListFor的方式来让用户选择已定列表中的一个数值。用法不复杂,这里简单做一个记录。
public class EricSunModel { public string ReadyTimeHour { get; set; } }
public ActionResult EricSunAction() { EricSunModel esModel = new EricSunModel(); esModel.ReadyTimeHour = "00"; GenerateReadyTimeViewData(); return View(esModel); } private void GenerateReadyTimeViewData() { ViewData["HourList"] = GetTimeHourList(); } private List<SelectListItem> GetTimeHourList() { List<SelectListItem> hourList = new List<SelectListItem>(); for (int i = 0; i < 24; i++) { if (i < 10) { hourList.Add(new SelectListItem { Text = "0" + i.ToString(), Value = "0" + i.ToString() }); } else { hourList.Add(new SelectListItem { Text = i.ToString(), Value = i.ToString() }); } } return hourList; }
@Html.DropDownListFor(m => m.ReadyTimeHour, ViewData["HourList"] as List<SelectListItem>)
@Html.DropDownListFor(m => m.ReadyTimeHour, ViewData["HourList"] as List<SelectListItem>, "---Select---")
@Html.HiddenFor(m => m.ReadyTimeHourList)
[HttpPost] public ActionResult EricSunAction(EricSunModel model) { if (!ModelState.IsValid) { GenerateReadyTimeViewData(); return View(model); } // model.ReadyTimeHour; return RedirectToAction("OtherActionName"); }
http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications
Asp.Net MVC中DropDownListFor的用法(转)
原文:http://www.cnblogs.com/0to9/p/5243756.html