开发者

Model binding for many to many relationship

开发者 https://www.devze.com 2023-03-09 09:34 出处:网络
I have tables like this License ------------ Id LicenseName DriversLicense ------------- Id Lic开发者_开发知识库enseId

I have tables like this

License
------------
Id
LicenseName


DriversLicense
-------------
Id
Lic开发者_开发知识库enseId
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.


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:

public partial class Driver
{
     public int Id { get; set; }

     // other properties

     [UIHint("Licenses")]
     public virtual ICollection<License> Licenses { get; set; }
}

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:

@inherits System.Web.Mvc.WebViewPage<IList<License>>
@Html.ListBoxFor(x => x,
                new MultiSelectList((List<License>)ViewBag.LicenseAll,
                "Id",
                "LicenseName",
                Model))

In your controller, set ViewBag.LicenseAll to all possible License options. In your Driver edit view, you just call:

@Html.EditorFor(m=>m.Licenses)

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.


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

DriversLicence
----------------
LicenceId
DriverId

Where both these columns compose complex primary key. If you left Id column in the table your DriversLicence will be exposed as other entity.

0

精彩评论

暂无评论...
验证码 换一张
取 消