MVC4绑定下拉列表列表中的(错误) [英] MVC4 binding drop down list in a list (bug)
问题描述
的我有一个视图下面code。 (index.cshtml)的
捆绑问题
- 第一个下拉选择正确的价值的基础上,ChildItem编辑模板。
- 使用内联第二个例子下拉列表中的不工作的,我不希望创建一个编辑模板,只是为了显示下拉值。
- 奇怪的是,TextBoxFor会显示正确的值。这似乎是一个问题,只是在下拉列表中。
- The first drop down selects the correct value, based on the 'ChildItem' editor template.
- The second example using an inline drop down list does not work I don't want to create an editor template just to display drop down values.
- The odd thing is that TextBoxFor will show the correct value. This seems to be an issue just with the drop down list.
如何解决的结合,第二下拉的作品?我调试了。这样看来,ViewData.Eval不是从拿起正确的值 _。儿童[I] .ChooseId
。
How do I fix the binding so that the second drop down works? I debugged it. It would appear that ViewData.Eval is not picking up the correct value from _.Children[i].ChooseId
.
更新(错误)结果
的这是一个确认的错误(低优先级,怎么样?)在MVC框架的http:/ /aspnet.$c$cplex.com/workitem/8311 的
@using (Html.BeginForm())
{
for (int i = 0; i < Model.Children.Count(); i++)
{
<p>A: @Html.EditorFor(_ => _.Children[i], "ChildItem")</p>
<p>B: @Html.DropDownListFor(_ => _.Children[i].ChooseId, TestModel.PeopleSelect)</p>
}
<button type="submit">GO</button>
}
我一直在使用 DropDownListFor试过(_ =&GT; Model.Children [I] .ChooseId)
,结果相同结果。
使用 TextBoxFor(_ =&GT; _.Children [I] .ChooseId)?
显示正确的值,奇怪
I have tried using DropDownListFor(_ => Model.Children[i].ChooseId)
, same result.
using TextBoxFor(_ => _.Children[i].ChooseId)
shows the correct value, wierd?
有关参考这里是 ChildItem.cshtml
@using dropdown.Controllers
@using dropdown.Models
@model dropdown.Models.TestPerson
@Html.DropDownListFor(_ => _.ChooseId, TestModel.PeopleSelect)
它看起来是这样的:
It looks like this:
推荐答案
我后来发现这一点:的 HTTP://aspnet.$c$cplex.com/workitem/8311
这是一个确认的错误。
I have subsequently found this: http://aspnet.codeplex.com/workitem/8311 It's a confirmed bug.
我已经找到了唯一的解决方法是这样的。
The only workaround i have found is this.
@ Html.DropDownListFor(_ =&GT; _.Children [I] .ChooseId,马克(TestModel.PeopleSelect,Model.Children [I] .ChooseId))
@functions {
private IEnumerable<SelectListItem> Mark(IEnumerable<SelectListItem> items, object Id)
{
foreach (var item in items)
if (string.CompareOrdinal(item.Value, Convert.ToString(Id)) == 0)
item.Selected = true;
return items;
}
}
这篇关于MVC4绑定下拉列表列表中的(错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!