多对多关系的模型绑定
我有这样的表格,
License
------------
Id
LicenseName
DriversLicense
-------------
Id
LicenseId
DriverId
Drivers
-----------
Id
FirstName
LastName
许可证表中有 6 个条目,现在我想为特定驱动程序的所有许可证选项创建带有是/否的单选按钮。表之间的最佳关系是什么? (一对一、一对多、多对多)以及如何使用模型绑定显示单选按钮。
I have tables like this
License
------------
Id
LicenseName
DriversLicense
-------------
Id
LicenseId
DriverId
Drivers
-----------
Id
FirstName
LastName
There are 6 enteries in License table, Now I want to create radio buttons with Yes/No for all License options for a particual driver. Whats the best relations between the table? (one-one, one-many, many-many) and how I will show radio buttons using model binding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
拉迪斯拉夫关于实体关系的说法是正确的。如果您希望此功能起作用,则必须从关联表中删除 Id 字段。此外,在数据模型中,每个实体都需要有一个引用其他集合的导航属性:
对于 Driver 类也是如此。然而,我认为尝试使用单选按钮效果不会很好。在我正在进行的一个项目中,我们有几种这样的情况,并且我们使用列表框。
在您希望用户选择的集合上使用 UIHint 并创建一个部分视图来保存该类型集合的列表框。因此,在
Licenses.cshtml
中 - 必须在 ~/Views/Shared/EditorTemplates/ 中创建,您将拥有如下内容:在控制器中,将
ViewBag.LicenseAll
设置为所有可能的值许可证选项。在您的驱动程序编辑视图中,您只需调用:您在模型上设置的 UIHint 将找到正确的部分视图,并显示所有许可证选项并选择为当前驱动程序设置的所有选项。
我知道这个答案并不能直接回答您的问题(使用单选按钮),但考虑到您的情况,这就是我的建议。
Ladislav is correct about the entity relationships. If you want this to work, you will have to drop the Id field from the associative table. In addition, in your data model, each entity needs to have a navigation property referencing the other collection:
And the same for Driver class. However, I think trying to use radion buttons will not work very well. In a project I'm working on we have several of these types of situations and we use a listbox.
Use UIHint on the collection you want the user to select and create a partial view to hold the list box for that type of collection. So in
Licenses.cshtml
- must be created in ~/Views/Shared/EditorTemplates/ you will have something like this:In your controller, set
ViewBag.LicenseAll
to all possible License options. In your Driver edit view, you just call:The UIHint you set on the model will find the correct partial view and will display all License options and select all those that are set for the current Driver.
I know this answer does not directly answer your question (using radio buttons), but given your situation, this is what I would recommend.
正如我在评论中发布的那样,您应该立即看到这一点。如果你不简单地停止工作并进行一些学习。
驱动程序和许可证之间是多对多,必须使用关系数据库 (DriversLicence) 中的联结表进行建模。从概念抽象出来的多对多被分解为Drivers和DriversLicence之间以及License和DriversLicence之间的两个一对多。
EFv4 能够直接使用多对多,但在您的情况下不行,因为您的联结表不仅包含外键列(LicenceId 和 DriverId),还包含附加列(Id)。仅当您使用 as 建模时,EF 才能隐藏联结表,
其中这两列都构成复杂的主键。如果您在表中留下 Id 列,您的 DriversLicence 将作为其他实体公开。
As I posted in the comment you should see this immediately. If you don't simply stop your work and do some learning.
This is many-to-many between Drivers and Licence which must be modelled with junction table in relational database (DriversLicence). Many-to-many from conceptual abstraction is broken to two one-to-many between Drivers and DriversLicence and between Licence and DriversLicence.
EFv4 is able to work with many-to-many directly but not in your case because your junction table doesn't contain only foreign keys columns (LicenceId and DriverId) but also additional column (Id). EF can hide junction table only if you model it with as
Where both these columns compose complex primary key. If you left Id column in the table your DriversLicence will be exposed as other entity.