使用IEnumerable的DropDownListFor是令人讨厌的,而不是DRY

时间:2015-11-25 17:02:33

标签: c# razor asp.net-mvc-5.2 dropdownlistfor

为什么这个Html Helper代码如此讨厌?我在linq表达式中指定要绑定的ID,但是必须在SelectList中再次执行此操作?有没有更好的方法来做到这一点,但仍然将RelationshipTypes保持为IEnumerable?

型号:

public class ResponderGuestsModel
{
    public List<GuestInfo> Guests { get; set; }
    public IEnumerable<Business.RelationshipType> RelationshipTypes { get; set; }

        public class GuestInfo
        {
            public int? RelationshipTypeId { get; set; }
        }
    }
}

查看:

@for (int i = 0; i < Model.Guests.Count; i++)
{
    @Html.DropDownListFor(model => model.Guests[i].RelationshipTypeId, new SelectList(Model.RelationshipTypes, "RelationshipTypeId", "Name", Model.Guests[i].RelationshipTypeId), "", new { @class = "form-control" })
}

0 个答案:

没有答案