我想创建一个部分,其中包含先前配置列表的选择。
控制器中的我可以创建SelectList
并将其添加到ViewBag
。
但是,在控制器上定义的ViewBag
不会传播到Partial。 Partials有自己的ViewBag
例如这个POCO项目类:
class MyItem{
[Key]
public int id {get;set;}
public int Forign_element_id {get;set;}
public string PropB {get;set;}
}
它的偏见是这样的:
@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)
重要。列表中的每个项都有一个Forign_element_id值。必须在渲染时的选择框中选择此值。
答案 0 :(得分:1)
使用接受SelectList
的{{3}}之一将additionalViewData
作为对象传递。
在主视图中(假设您的模型具有名为MyProperty
的属性,其类型为MyItem
)
@Html.EditorFor(m => m.MyProperty, new { options = (SelectList)ViewBag.MySelectList })
并在您的MyItem.cshtml
模板中
@Html.DropDownListFor(m => m.Forign_element_id, (SelectList)ViewData["options"])
答案 1 :(得分:1)
在您的编辑器模板中尝试此操作:而不是(词典)将其强制转换为您的类型;就我而言,我从调用层传递了Dictionary;所以我用它作为新的SelectList(列表,“Key”,“Value”),相应地改变
编辑模板名称 - 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%" })
编辑 - 添加调用代码
@Html.EditorFor(model => model.YourModel, "DropdownList", new { List = ViewBag.YourList, DefaultText = "Select one item"})
其中“DropdownList”是编辑器模板名称
答案 2 :(得分:0)
控制器:
let beginJumpAction = //move the player upwards slightly
let smallJumpEndAction = //begin to move the player down
let bigJumpEndAction = //move the player up a bit more and then move down
let blockAction = SKAction.run {
if touchStillDown {
player.runAction(bigJumpEndAction)
} else {
player.runActgion(smallJumpEndAction)
}
}
player.runAction(SKAction.sequence([beginJumpAction, blockAction])
查看:
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});
}