C# @剃须刀的行动
我正在尝试在asp.net mvc 3应用程序中为我的DropDownList Box创建一个partialview。 在我的页面中,我有:C# @剃须刀的行动,c#,visual-studio-2010,asp.net-mvc-3,C#,Visual Studio 2010,Asp.net Mvc 3,我正在尝试在asp.net mvc 3应用程序中为我的DropDownList Box创建一个partialview。 在我的页面中,我有: @Html.Action("PopulateCombo","ComboController") 控制器部分视图: public ActionResult PopulateCombo() { //some code here to organise data and maybe some caching return ItemsForCom
@Html.Action("PopulateCombo","ComboController")
控制器部分视图:
public ActionResult PopulateCombo()
{
//some code here to organise data and maybe some caching
return ItemsForCombo;
}
是否有更好的模板化Dropdownlistbox的方法?我将在控制器中填充数据,然后将其传递给模型,最后使用
Html.DropDownListFor
。我的意思是,这就是MVC的含义:)
示例
(附言:我已经很久没有用c#编码了,所以对于任何类型的打字错误,我深表歉意)
控制器
Public ActionResult ()
{
Model m = new Model();
//populate data into a list
m.Items = ...
return View(m);
}
model.cs
...
Public List<object> Items { get; set; }
你的要求是什么?局部视图可以表示可以在整个应用程序中重复使用的控件。我不认为下拉列表是局部视图的好候选 如果我是你,我想显示一个下拉列表,我会使用现有的HTML助手 在我的控制器中,我将在视图包中存储下拉列表的值:
IList<SelectListItem> selectListItems = new List<SelectListItem>();
// Populate selectListItems
...
// Create a select list. You'll have to replace dataValueField and dataTextField with property names
SelectList mySelectList = new SelectList(selectListItems, "dataValueField", "dataTextField");
// Assume that select list contains a list of countries
ViewBag.Countries = mySelectList;
我是在记事本上写的,所以可能有语法错误
IList<SelectListItem> selectListItems = new List<SelectListItem>();
// Populate selectListItems
...
// Create a select list. You'll have to replace dataValueField and dataTextField with property names
SelectList mySelectList = new SelectList(selectListItems, "dataValueField", "dataTextField");
// Assume that select list contains a list of countries
ViewBag.Countries = mySelectList;
@Html.DropDownListFor(m => m.CountryId, (SelectList) ViewBag.Countries);