多对多的复选框和MVC [英] Many to many checkbox and mvc

查看:225
本文介绍了多对多的复选框和MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架4,我的模型关系会自动从查找表产生的。我的模型由请求建筑。请求可以有很多建筑和建筑可以与许多请求关联。我发现如何DropDownFor基于模型的关系自动选择一个项目几个职位。但是的HtmlHelper CheckBoxFor 想要一个前pression返回布尔。我的模型不具备,因为它是基于关系的检查表明一个布尔值。

I'm using Entity Framework 4 and my model relationships are automatically generated from the lookup table. My models consist of Request and Building. A request can have many buildings, and a building can be associated with many requests. I've found a few posts on how DropDownFor automatically selects an item based on model relationships. But the HtmlHelper CheckBoxFor wants an expression that returns bool. My models don't have a bool indicating checked because it is based on the relationship.

任何人有技巧或经验?

推荐答案

千万不要错过你的EF模型视图。定义这是专门针对给定视图的需求类视图模型。你不需要很多一对多视图递归关系。所以,在你想生成一个复选框,你会对你的视图模型对应的布尔属性的情况。它是将查询资料库,获取EF车型,它们映射到视图模型(该任务可与框架如的 AutoMapper ),最后通过视图模型视图,以便在你看来,你干脆:

Don't pass your EF models to the view. Define view models which are classes specifically tailored to the needs of a given view. You don't need many-to-many recursive relations in the view. So in the case where you want to generate a checkbox you would have a corresponding boolean property on your view model. It's the controller that would query the repository, fetch the EF models, map them to the view model (this task could be simplified with frameworks such as AutoMapper) and finally pass the view model to the view so that in your view you simply:

@Html.CheckBoxFor(x => x.SomeBooleanProperty)

如果你想有一个复选框列表,然后您的视图模型将包含某种类型的集合属性,将持有的布尔属性。

And if you wanted to have a list of checkboxes then your view model would contain a collection property of some type that will hold the boolean property.

这篇关于多对多的复选框和MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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