首页 文章

如何使用IEnuernation <T>属性向模型添加创建和编辑视图?

提问于
浏览
0

假设我使用Entity Framework 6并在模型优先方法中使用以下两个表:

enter image description here

这会创建以下代码:

enter image description here

enter image description here

我现在的问题是,如何在创建视图中使用IEnumeration属性?在详细信息或删除视图中,我只是使用Html.DisplayFor帮助器迭代它们,但我不知道我可以使用什么来实现创建和编辑视图的可比较 . 像这样的东西:

<div class="form-group">
    @Html.LabelFor(model => model.MappedEmployees, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <!--Something like checkbox list with Name property of each collection property item-->
    </div>
</div>

所以我最终会得到类似的东西:

enter image description here

1 回答

  • 0

    关于这个主题有一些很好的博客文章,它们应该对你有用:

    haacked.com blog posting

    Hanselman.com blog posting

    我没有测试下面的内容,但是这样的话:

    <div class="form-group">
        @Html.LabelFor(model => model.MappedEmployees, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
    
    @for(int i = 0; i < model.MappedEmployees.Count; i ++)
    {
    Html.CheckBoxFor(model => model.MappedEmployees[i],  new { htmlAttributes = new { @class = "form-control" } })
    }
    
        </div>
    </div>
    

相关问题