如何将DropDownListFor绑定到IList <t>

时间:2017-03-22 10:13:33

标签: c# asp.net-mvc list model-binding html.dropdownlistfor

我正在尝试将IList属性绑定到DropDownListFor()。将显示该列表,下拉列表中的数据将成功发布到数据库,但是再次加载页面时不会显示该数据。未根据ClubTeamID的值选择正确的选项。始终选择默认值( - )。页面中的其余输入,按钮和复选框在保存和重新加载时工作正常。还有其他我想念的东西吗?

我的观点(相关守则):

@model IList<SolidarityContract>
...
@if (Model != null)
{
    for (var i = 0; i < Model.Count; i++)
    {
    ...

        <td>@Html.DropDownListFor(model => Model[i].ClubTeamID,Model[i].TeamsList, "--", htmlAttributes: new { @class = "form-control" })</td>
        <td>@Html.EditorFor(model => Model[i].Club, new { htmlAttributes = new { @class = "form-control input-sm" } })</td>
    ...
    }
}

我的班级(相关属性):

public class SolidarityContract{

    public SolidarityContract()
    {
        TeamsList = SelectLists.TeamsGet();
    }

    public SelectList TeamsList { get; set; }
    public Nullable<long> ClubTeamID { get; set; }
    ...

}

SelectList:

public static  SelectList TeamsGet()
    {
        using (var db = new OlympiacosPlayerMSDBEntities())
        {

            IEnumerable<CustomSelectList> cpd = (from comp in db.Team
                                                 where comp.IsDeleted == false
                                                 select new CustomSelectList 
                                                 { ID = comp.ID.ToString(), 
                                                     Description = (comp.TeamCategoryID==1?"ΠΕΛ: ":"ΠΡΟ: ") +comp.Name }).ToList<CustomSelectList>().OrderBy(c => c.Description);
            return new SelectList(cpd, "ID", "Description");
        }
    }

0 个答案:

没有答案