I have 2 tables Projects and Employees, they need a many to many relation so I created another table Projects_Employees with an id, a project_id and an employee_id
Does anyone know the correct way to use this in MVC3 or have existing code or a tutorial for this?
Basicly i want to be able to create a new project (it has a name, date and employees) select the employees with tick boxes and save it.
When I delete an employee i'm not gonna delete it from the DB but simply set the bit field visible to false so it doesn't show up in the tickboxes again if a new project has to be created.
What is the best way to setup the models controllers and view?
I'm an absolute noob in MVC :) Do you have some existing code that you can provide me with? models view and controller?
I don't have any existing code on-hand to walk you thru this.
Your models are going to be simple:
ProjectClass - int ProjectId, string ProjectName
EmployeeClass = int EmpId, string EmpName, bool IsActive // IsActive is your "visible" column true/false
RelClass = int RelId, ProjId, EmpId // this is your many to many class (Rel = relationships for naming purposes)
Here's how you'll handle the checkboxes:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Everything you require for this project can be obtained with a little research on your favorite search engine.
BorrieRulez
Member
18 Points
60 Posts
MVC3 and Many to Many Relations SQL
Apr 06, 2012 04:32 PM|LINK
Hey everyone,
I have 2 tables Projects and Employees, they need a many to many relation so I created another table Projects_Employees with an id, a project_id and an employee_id
Does anyone know the correct way to use this in MVC3 or have existing code or a tutorial for this?
Basicly i want to be able to create a new project (it has a name, date and employees) select the employees with tick boxes and save it.
When I delete an employee i'm not gonna delete it from the DB but simply set the bit field visible to false so it doesn't show up in the tickboxes again if a new project has to be created.
What is the best way to setup the models controllers and view?
Tx,
Borrie
JohnLocke
Contributor
3014 Points
689 Posts
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 04:43 PM|LINK
When you return the list of employees to "Checkmark" for your project, use the where() filter to only include visible employees.
validEmployees = employeeList.where(e => e.visible == true);
That generic code will only select employees who are currently "visible" = true.
BorrieRulez
Member
18 Points
60 Posts
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 04:47 PM|LINK
John,
I'm an absolute noob in MVC :) Do you have some existing code that you can provide me with? models view and controller?
JohnLocke
Contributor
3014 Points
689 Posts
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 05:21 PM|LINK
I don't have any existing code on-hand to walk you thru this.
Your models are going to be simple:
ProjectClass - int ProjectId, string ProjectName
EmployeeClass = int EmpId, string EmpName, bool IsActive // IsActive is your "visible" column true/false
RelClass = int RelId, ProjId, EmpId // this is your many to many class (Rel = relationships for naming purposes)
Here's how you'll handle the checkboxes:
http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx
Everything you require for this project can be obtained with a little research on your favorite search engine.
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 06:44 PM|LINK
Creating an Entity Framework Data Model for an ASP.NET MVC Application
BorrieRulez
Member
18 Points
60 Posts
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 06:48 PM|LINK
Ricka6,
Thanks, i was just looking in to that example, do you also know of an example which has a sql server database first approach?
(A lot of the tutorials are code first)
Borrie
BorrieRulez
Member
18 Points
60 Posts
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 07:27 PM|LINK
I followed this tutorial to get my database into my project:
http://msdn.microsoft.com/en-us/data/gg685489
It creates some models but I was wondering do I need all that and how can I configure these things manually?
I'm a bit confused here...
JohnLocke
Contributor
3014 Points
689 Posts
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 07:31 PM|LINK
Check out the "Nerd Dinner" tutorial for a great jump-start on LINQ to SQL. I prefer LINQ over other data connection models.
Best of luck
BorrieRulez
Member
18 Points
60 Posts
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 07:51 PM|LINK
John,
I'll check it out but to be honest I'm getting more and more confused :)
Borrie
ricka6
All-Star
15070 Points
2272 Posts
Microsoft
Moderator
Re: MVC3 and Many to Many Relations SQL
Apr 06, 2012 11:07 PM|LINK
Please don't. For new projects EF is much preffered. Nerd Dinner is ancient technology. Please go through
MVC 3 Intro to ASP.NET MVC
MVC 4 Beta Intro to ASP.NET MVC
Intro to ASP.NET MVC 4 Beta with Visual Studio 11 Beta
Then go through Music Store, then
Creating an Entity Framework Data Model for an ASP.NET MVC Application
If you're up for MVC 4/Beta, I can send you Music store for MVC 4/Beta