ASP.NET MVC4模型的子集合下拉列表未正确绑定 [英] ASP.NET MVC4 Model's Child Collection Drop Down List not binding properly

查看:85
本文介绍了ASP.NET MVC4模型的子集合下拉列表未正确绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到与此此处我相信,但是解决方法对我而言不起作用.我的问题是我在主视图的ViewModel中有一个子模型集合.它们包含要在两个字段(下拉列表和密码字段)中显示的数据.每个下拉列表选择必须唯一.所有内容均已保存并正确发送到视图,但是调用视图时下拉列表未绑定到所选值,但密码字段已绑定.它们都默认为第一个选择,即使它们要绑定到的属性是唯一的,并且只有一个可以成为第一个值.任何帮助或见解表示赞赏.谢谢.

I'm having the same issue as this here I believe, but the workaround is not working for me. My issue is that I have a child collection of models inside my main view's ViewModel. They contain data to be displayed in two fields, a dropdownlist and a password field. Each dropdownlist selection must be unique. Everything is saving and being sent to the view properly, however the dropdownlist are not binding to the selected values when the view is called but the password field is. They all default to the first selection, even though the property they are suppose to bind to is unique and only one can be the first value. Any help or insight is appreciated. Thanks.

在我看来,这是发生问题的部分.我已注释掉我的努力,并尝试了以上链接的变通方法,但无济于事:

Here is the part in my view where the issue is occurring. I have commented out my efforts and tried the above link's workaround to no avail:

@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;
}
}



@for (int j = 0; j < Model.PasswordResetQuestionUserAnswers.Count(); j++)
{
@Html.Hidden("PasswordResetQuestionUserAnswers.Index", j)
@Html.HiddenFor(p => Model.PasswordResetQuestionUserAnswers[j].Id)
@Html.HiddenFor(p => Model.PasswordResetQuestionUserAnswers[j].UserId)
<div class="form-group">
<label class="col-md-2 control-label">Password Reset Question @(j+1)</label>
<div class="col-md-6">
@*@Html.DropDownList("PasswordResetQuestionUserAnswers[" + j + "].PasswordResetQuestionId", Model.PasswordResetQuestionList, new { @class = "form-control passwordQuestion" })*@
@*@Html.DropDownListFor(x => Model.PasswordResetQuestionUserAnswers[j].PasswordResetQuestionId, Model.PasswordResetQuestionList, new { @class = "form-control passwordQuestion" })*@
@Html.DropDownListFor(x => Model.PasswordResetQuestionUserAnswers[j].PasswordResetQuestionId, Mark(Model.PasswordResetQuestionList, Model.PasswordResetQuestionUserAnswers[j].PasswordResetQuestionId))
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Password Reset Answer @(j+1)</label>
<div class="col-md-6">
@Html.Password("PasswordResetQuestionUserAnswers[" + j + "].Answer", Model.PasswordResetQuestionUserAnswers[j].Answer, new { @class = "form-control passwordQuestionUserAnswer" })
@*@Html.PasswordFor(x => Model.PasswordResetQuestionUserAnswers[j].Answer, new { @class = "form-control passwordQuestionUserAnswer" })*@
</div>
</div>
}

推荐答案

我只是遇到了同样的问题.这种语法对我有用:

I just had this same problem. This syntax works for me:

 @Html.DropDownListFor(x => x.ChildCollection[i].ChildID, new SelectList(ViewBag.ChildCollectionSelect as SelectList, "Value", "Text", Model.ChildCollection[i].ChildID))

将SelectList定义为新的,然后从模型中专门设置选定的值.

Define the SelectList as new, then specifically set the selected value from the model.

这篇关于ASP.NET MVC4模型的子集合下拉列表未正确绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆