Click here to Skip to main content
15,119,786 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two tables as seen below with a foreign key on MainMember. Each member may have a dependent. I want to insert data into the main member table using only text boxes. Not drop downs, including the foreign key attributes

What I have tried:

public class MainMember
{
public int Id { get;set;}
public string Name { get;set;}
public string PassportNumber { get;set;}


[ForeignKey("Dependent")]
public int DepId { get;set;}
public string DepName { get;set;}
public string DepPassportNumber { get;set;}
public Dependent Dependent {get;set;}
}


public class Dependant
{
public int Id { get;set;}
public string Name { get;set;}
public string PassportNumber {get;set}
}

I am developing an mvc application using tdd, so I have a business logic method named Insert which inherits from my repository folder

public interface IMaimMemberRepository : IDsposable
{

void Insert(Order model);

}

Thereafter I implement the above method in my MainMemberRepository class.

public void Insert(MainMemberViewModel model)
{

using(var db = new MainMemberRepository())
{
db.Insert(new MainMember
{
Name= model.Name,
PassportNumber = model.PassportNumber,
DepName= model.DepName,
DepPassportNumber = model.PassportNumber
};

)

In my controller

public ActionResult Insert(MainMemberViewModel model)
{

var business = new MainMemberBusiness();

business.Insert(model);

return View():

}
Posted
Updated 7-Jul-18 7:34am

1 solution

The way you have it set up your members can only have one dependent. I would suggest the following setup instead.
C#
[Key]
public int MemberId {get;set;}
public string Name {get;set;}
public string PassportNumber {get;set;}
public ICollection<Dependent> Dependents {get;set;}

Your dependent should be like this
C#
[Key]
public int DependentId {get;set;}
[ForiegnKey("Member")]  {get;set;}
public int MemberId  {get;set;} // marks the member I belong to
public string Name {get;set;}
public string PassportNumber {get;set;}
public virtual Member Member {get;set;}

I always use ids like MemberId and DependentId so that its purpose is clear. Not a requirement, a personal preference. If you're looking through code for all the places where you used something, it's handy to have that full name to search.
So each Dependent has the "MemberId" in it so that you (or Entity Framework or some other ORM) can tie all the related Dependents to the Member.
Your Member class will have a ICollection<dependent> as one of its properties. The Dependent would have a Member property.
To deal with the foreign key you have to save the member and get the member id after it's saved, then set the MemberId in each of the Dependents before trying to save it. An ORM like Entity Framework with do this for you.
(there's another option that uses anything that's common between the two in a separate table called 'Person' and Member & Dependent each extend from that.)

As to your question about filling the items with text boxes, you're not showing any of the view code so I'm not sure how to help there.
Mike
   
Comments
Zain Nabi 8-Jul-18 15:55pm
   
I am using razor
Mike V Baker 8-Jul-18 23:55pm
   
That's not enough. You need to post the view and the controller code for the particular page and where you're having a problem.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900