我做了一件非常简单的事情并且没有工作。
@Html.DropDownListFor(m => m.ContentDefinitionID, Model.ContentBoxesSelectList, new {@class = "form-control"})
ContentDefinitionID
是UInt64
(虽然我已尝试过int)
我在页面上为4个不同的控件使用相同的选择列表。
如果我的Model.ContentDefinition
设置为4(在下拉列表中为test4
),那么它应该从模型中提取所选的值,而不是从selectList
权限中提取?有人说当你使用SelectList
语法时它会忽略m=>m.X
上的值 - 这是有道理的。
但它总是选择第一个。我尝试为它应该选择的内容添加另一个参数,但它想要文本,而不是值,我讨厌必须查找文本。 (而且我不确定如果我这样做,这将会正确地回复)
我即将创建一些JQuery代码来设置所有内容的默认值 - 但这很疯狂。这是DropDownListFor()
方法的显而易见的行为,类似于' duh'事 - 为什么它不起作用?我是否需要为页面上存在的每个控件创建自定义SelectList
?
---更新,模型等:
class PageModel
{
Int64 ContentDefinitionID {get;set;}
SelectList ContentBoxesSelectList {get;set;}
}
Controller init for model:
model.ContentDefinitionID = 4; // In real situation, this is an
array of N model.ContentBoxesSelectList = new SelectList( from
ContentDefinitionDocument doc in allContentBoxes
where doc.Size == size
select new {Name = doc.Name, id = doc.DefinitionID}, "Id", "Name");
渲染输出(从它作为数组):
selected value should be: 1
<select class="form-control" data-val="true" data-val-number="The field ContentDefinitionID must be a number." data-val-required="The ContentDefinitionID field is required." id="ContentBoxes_0__ContentDefinitionID" name="ContentBoxes[0].ContentDefinitionID" style="width:25%;float:left"><option value="1">Test1</option>
<option value="2">Test 2</option>
<option value="4">Test 4</option>
<option value="0">Test 0</option>
</select>
并且没有人被选中。
答案 0 :(得分:2)
从你生成的html(name="ContentBoxes[0].ContentDefinitionID"
)开始,你在for
循环中使用它。很遗憾DropDownListFor()
在for
循环中无法正常工作,您需要为模型使用自定义EditorTemplate
(已将其报告为错误但尚未修复)。
您尚未发布模型,但假设
public class ContentBox
{
public int ContentDefinitionID { get; set; }
....
}
public class ContentBoxesVM
{
public IEnumerable<ContentBox> ContentBoxes { get; set; }
public SelectList ContentBoxesSelectList { get; set; }
}
旁注:只需要生成一个SelectList
(而不是集合中每个对象的一个)
控制器
public ActionResult Edit()
{
ContentBoxesVM model = new ContentBoxesVM();
// populate model, for example
model.ContentBoxes = new List<ContentBox>()
{
new ContentBox { ContentDefinitionID = 4 }
};
model.ContentBoxesSelectList = new SelectList(...);
return View(model);
}
编辑模板(/Views/Shared/EditorTemplates/ContentBox.cshtml
)
@model ContentBox
....
@Html.DropDownListFor(m => m.ContentDefinitionID, (SelectList)ViewData["contentBoxesList"])
...
主视图
@model ContentBoxesVM
@using(Html.BeginForm())
{
....
// pass the select list as additional view data to the editor template
@Html.EditorFor(m => m.ContentBoxes, new { contentBoxesList = Model.ContentBoxesSelectList })
....
<input type="submit" />
}
答案 1 :(得分:0)
您可以通过更改模型来实现您想要的目标:
class PageModel
{
Int64 ContentDefinitionID {get;set;}
List<ContentDefinitionDocument> ContentBoxesList {get;set;}
}
并在控制器中:
array of N model.ContentBoxesList = allContentBoxes.Where(doc => doc.Size == size).ToList();
并在View中以这种方式创建SelectList
:
@Html.DropDownListFor(m => m.ContentDefinitionID,
new SelectList(Model.ContentBoxesSelectList,
"DefinitionID",
"Name",
Model.ContentDefintionID),
new {@class = "form-control"})