[英]ASP.NET MVC. How insert a SelectList inside an Partial?
I would like to create an partial which holds a select for a previously configured list. 我想创建一个局部零件,其中包含一个先前配置的列表的选择。
in the controller I'm able to create the SelectList
and add it to ViewBag
. 在控制器中,我能够创建
SelectList
并将其添加到ViewBag
。 However ViewBag
defined on controller are not propagated to the Partial. 但是,在控制器上定义的
ViewBag
不会传播到Partial。 Partials have its own ViewBag 局部变量有自己的ViewBag
for instance this POCO item class: 例如,此POCO项目类:
class MyItem{
[Key]
public int id {get;set;}
public int Forign_element_id {get;set;}
public string PropB {get;set;}
}
And its Partial like this: 它的部分是这样的:
@model MyItem
@Html.HiddenFor(model => model.id)
@Html.LabelFor(model => model.PropA)
@Html.DropDownListFor(model => model.Forign_element_id, (SelectList)ViewBag.MySelectList)
//Error! ViewBag are not propageted to the EditorTemplate
@Html.LabelFor(model => model.PropB)
@Html.EditorFor(model => model.PropB)
Important. 重要。 Each item in the list has a Forign_element_id value.
列表中的每个项目都有一个Forign_element_id值。 This values must be selected in select box at rendering.
渲染时必须在选择框中选择此值。
Pass the SelectList
as an object using one of the overloads that accepts additionalViewData
. 传递
SelectList
作为使用的一个对象重载接受additionalViewData
。
In the main view (assumes your model has a property named MyProperty
which is typeof MyItem
) 在主视图中(假设您的模型具有一个名为
MyProperty
的属性,该属性为typeof MyItem
)
@Html.EditorFor(m => m.MyProperty, new { options = (SelectList)ViewBag.MySelectList })
and in your MyItem.cshtml
template 并在您的
MyItem.cshtml
模板中
@Html.DropDownListFor(m => m.Forign_element_id, (SelectList)ViewData["options"])
Try this in your editor template: Instead of (Dictionary) cast it to your type; 在您的编辑器模板中尝试以下操作: 代替(Dictionary)将其强制转换为您的类型; in my case, I was passing Dictionary from the calling layer;
就我而言,我是从调用层传递Dictionary的。 so I have used it as new SelectList(list, "Key", "Value"), change accordingly
所以我将其用作新的SelectList(list,“ Key”,“ Value”),相应地进行更改
Name of Editor Template - DropdownList 编辑器模板的名称-DropdownList
@{
var defaultText = (string)ViewData["DefaultText"];
//var list = (Dictionary<string, string>) ViewData["List"]; -- my List was Dictionary
var list = (List<YourType>) ViewData["List"];
}
@Html.DropDownListFor(model => model,
new SelectList(list, "Key", "Value"),
defaultText,
new { @class = "form- control", style="height: auto;width:90%" })
EDIT - adding calling code 编辑-添加调用代码
@Html.EditorFor(model => model.YourModel, "DropdownList", new { List = ViewBag.YourList, DefaultText = "Select one item"})
where "DropdownList" is editor template name 其中“ DropdownList”是编辑器模板名称
controller: 控制器:
private IEnumerable<SelectListItem> GetMyItemsSelectItems()
{
return (from s in _rep.GetMyItems()
select new SelectListItem
{
Text = s.MyItemName,
Value = s.ID.ToString()
}).AsEnumerable();
}
public ActionResult Create()
{
ViewBag.MyItems = GetMyItemsSelectItems();
return View(new MyItem { ID = 1, Forign_element_id = 2});
}
View: 视图:
@model App.Models.MyItem
@Html.DropDownListFor(m => m.ID, new SelectList(ViewBag.MyItems,
"Value", "Text"))
@Html.DropDownListFor(m => m.Forign_element_id,
new SelectList(ViewBag.MyItems, "Value", "Text"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.