I’m just learning MVC and have a question that I’m sure is obvious to everyone but me ;-). I’m using MVC3 with EF4 and DbContext and have good success at understanding most of the basic functionality. Here’s my problem:
Lets say I have 2 tables (Company & User) and a 1-to-many relationship between them (foreign Key Company Id).
My question is this: What is the best way of enforcing the 1-to-many relationship in MVC? I don’t want anyone in any Company to edit any User. I want only the people in Company X to modify their own Users.
I’m trying to create a new User in a particular Company. I don’t want to use the standard EF template (which gives you a Company Drop Down List since its a foreign key) in the User View. I want to make sure that I’m in /Company/123456 before I can create a User from within that company.
I looked at the ASP.NET routing to see if something like this is possible:
/Company/123456/User/Create/ – it doesn’t look like it. (looks like I can only have one Controller/Action/Id). I’m just not sure how I ‘remember’ that we are currently in Company 123456 when creating new object.
Querystrings? (/User/Create/?Company=123456). Should I create another class (CompanyUser)? If so, I’m unsure how the routing would look (CompanyUser/Create/123456)?
Thanks in advance for the advice.
Mike
For /Company/123456/User/Create/ it should be configured as:
In this case, /Company/123456/User/Create/ will go to an action that will look like