简体   繁体   中英

I want to save an ID from a table to another table using asp.net mvc 5

I have a problem in my project ASP.NET MVC 5 on CREATE ACTION. Here's my code :

Models

Members:

  public partial class Member
   {
   public Member()
    {
        this.Acc_Transactions = new HashSet<Acc_Transactions>();
        this.Addresses12 = new HashSet<Addresses1>();
        this.BankingDetails = new HashSet<BankingDetail>();
        this.Contacts = new HashSet<Contact>();
        this.TalentCommitments = new HashSet<TalentCommitment>();
        this.Pledges = new HashSet<Pledge>();
    }

    public int m_id { get; set; }
    public int title_id { get; set; }
    public string initial { get; set; }
    public string fname { get; set; }
    public string lname { get; set; }
    public Nullable<System.DateTime> dob { get; set; }
    public string maritial { get; set; }
    public string religion { get; set; }
    public string occupation { get; set; }
    public string company { get; set; }
    public string Note { get; set; }
    public Nullable<int> Memtype_Id { get; set; }
    public string employed { get; set; }
    public Nullable<System.DateTime> reg_date { get; set; }
    public string AccNumb { get; set; }
    public string Hnumber { get; set; }
    public Nullable<bool> Active { get; set; }
    public string AgeGrp { get; set; }
    public int h_id { get; set; }
    public Nullable<int> postal_addid { get; set; }
    public Nullable<int> phys_addid { get; set; }
    public Nullable<int> maritialid { get; set; }
    public Nullable<bool> PlndGv { get; set; }

    public virtual ICollection<Acc_Transactions> Acc_Transactions { get; set; }
    public virtual Addresses1 Addresses1 { get; set; }
    public virtual Addresses1 Addresses11 { get; set; }
    public virtual ICollection<Addresses1> Addresses12 { get; set; }
    public virtual ICollection<BankingDetail> BankingDetails { get; set; }
    public virtual ICollection<Contact> Contacts { get; set; }
    public virtual Head Head { get; set; }
    public virtual Maritial Maritial1 { get; set; }
    public virtual ICollection<TalentCommitment> TalentCommitments { get; set; }
    public virtual MemberType MemberType { get; set; }
    public virtual ICollection<Pledge> Pledges { get; set; }
    public virtual Title Title { get; set; }
}

}

Heads:

public partial class Head
 {
    public Head()
    {
        this.Addresses1 = new HashSet<Addresses1>();
        this.Members = new HashSet<Member>();
    }

    public int h_id { get; set; }
    public string h_initials { get; set; }
    public string fname { get; set; }
    public string lname { get; set; }
    public string Email { get; set; }
    public string cell { get; set; }
    public string cell2 { get; set; }
    public string tel_h { get; set; }
    public string tel_w { get; set; }
    public string fax { get; set; }
    public string h_no { get; set; }
    public int title_id { get; set; }
    public Nullable<bool> active { get; set; }

    public virtual ICollection<Addresses1> Addresses1 { get; set; }
    public virtual ICollection<Member> Members { get; set; }
    public virtual Title Title { get; set; }
}

}

ViewModel

 public class MembersViewModel
{
    public int m_id { get; set; }
    public string titles { get; set; }
    public string initial { get; set; }
    public string fname{ get; set; }
    public string lname { get; set; }
    public string email { get; set; }
    public Nullable<System.DateTime> dob { get; set; }
    public string maritials { get; set; }
    public string religion { get; set; }
    public string occupation { get; set; }
    public string company { get; set; }
    public string note { get; set; }
    public string employed { get; set; }
    public Nullable<System.DateTime> regdate { get; set; }
    public string accNumb { get; set; }
    public string hnumber { get; set; }
    public string agegroup { get; set; }
    public string plandGv { get; set; }
    public string cell { get; set; }
    public string tel_h { get; set; }
    public int title_id { get; set; }
    public string flatName { get; set; }
    public string flatNo { get; set; }
    public string strname { get; set; }
    public string strNo { get; set; }
    public string suburb { get; set; }
    public string city { get; set; }
    public string tel_w { get; set; }
    public string fax { get; set; }
    public string cell2 { get; set; }
    public bool active { get; set; }
    public string province { get; set; }
    public string country { get; set; }
    public int? postalcode { get; set; }
    public string zone { get; set; }
    public bool isHa { get; set; }
    public int addtype { get; set; }
    public int PhysAddID { get; set; }
    public Nullable<int> phys_addid { get; set; }
    public int h_id { get; set; }
    public int maritialid { get; set; }
    public int Memtype_Id { get; set; }

}

}

Controller

Get Action:

  public ActionResult Create()
    {
        ViewBag.phys_addid = new SelectList(db.Addresses1, "PhysAddID", "strNo");
        ViewBag.postal_addid = new SelectList(db.Addresses1, "PhysAddID", "strNo");
        ViewBag.h_id = new SelectList(db.Heads, "h_id", "h_initials");
        ViewBag.maritialid = new SelectList(db.Maritials, "Maritialid", "MaritialType");
        ViewBag.Memtype_Id = new SelectList(db.MemberTypes, "Memtype_Id", "Type");
        ViewBag.title_id = new SelectList(db.Titles, "title_id", "Titles");
        return View();
    }

Post Action:

public ActionResult Create(MembersViewModel memberViewModel)
    {
        var client = new Member
        {
            fname = memberViewModel.fname,
            lname = memberViewModel.lname,
            initial = memberViewModel.initial,
            title_id = memberViewModel.title_id,
            dob = memberViewModel.dob,
            maritial = memberViewModel.maritials,
            religion = memberViewModel.religion,
            occupation = memberViewModel.occupation,
            company = memberViewModel.company,
            Note = memberViewModel.note,
            employed = memberViewModel.employed,
            reg_date = memberViewModel.regdate,

            AccNumb = memberViewModel.accNumb,
            Hnumber = memberViewModel.hnumber,
            Active = memberViewModel.active,
            AgeGrp = memberViewModel.agegroup,
            h_id = memberViewModel.h_id,
        };
        var client1 = new Addresses1();
        var contact = new Contact();
        using (var context = new ParishDBSQLEntities())
        {
            context.Members.Add(client);                
            client1.h_ID = client.h_id;
            client1.strNo = memberViewModel.strNo;
            client1.strname = memberViewModel.strname;
            client1.Suburb = memberViewModel.suburb;
            client1.City = memberViewModel.city;
            client1.Province = memberViewModel.province;
            client1.Country = memberViewModel.country;
            client1.PostalCode = memberViewModel.postalcode;
            client1.zone = memberViewModel.zone;
            client1.flatName = memberViewModel.flatName;
            client1.flatNo = memberViewModel.flatNo;
            client1.IsHa = memberViewModel.isHa;
            client1.AddType = memberViewModel.addtype;
            context.Addresses1.Add(client1);
            contact.Email = memberViewModel.email;
            contact.cell = memberViewModel.cell;
            contact.cell2 = memberViewModel.cell;
            contact.tel_h = memberViewModel.tel_h;
            contact.tel_w = memberViewModel.tel_w;
            contact.fax = memberViewModel.fax;
            contact.m_id = client.m_id;
           context.Contacts.Add(contact);
            context.SaveChanges();
        }

my problem is that i want to save a new member on members table but the new member must save a head ID from heads table can you please assist. In details"
H_id is a foreign key to members table from heads tables . The head is already saved, now i want save a new member on members table under the head that already exist in heads table. but on members table i want to save H_id of that particular head."

From our comments, in your Post action I would suggest using Bind for all data that is going to be posted to the DB. You just need to mention field names (for the example I only mentioned 3 fields).

public ActionResult Create([Bind(Include = "fname,lname,cell")]MembersViewModel memberViewModel)
{
    Head head = context.Heads.FirstOrDefault(x => x.cell.Equals(memberViewModel.cell));
    // now we have a Head entity to reference
    var client = new Member
    {
        fname = memberViewModel.fname,
        lname = memberViewModel.lname,
        initial = memberViewModel.initial,
        title_id = memberViewModel.title_id,
        dob = memberViewModel.dob,
        maritial = memberViewModel.maritials,
        religion = memberViewModel.religion,
        occupation = memberViewModel.occupation,
        company = memberViewModel.company,
        Note = memberViewModel.note,
        employed = memberViewModel.employed,
        reg_date = memberViewModel.regdate,

        AccNumb = memberViewModel.accNumb,
        Hnumber = memberViewModel.hnumber,
        Active = memberViewModel.active,
        AgeGrp = memberViewModel.agegroup,
        h_id = head.h_id,                      // this is where we set Member.h_id
    };
    var client1 = new Addresses1();
    var contact = new Contact();
    using (var context = new ParishDBSQLEntities())
    {
        context.Members.Add(client);                
        client1.h_ID = head.h_id;               // using head entity as reference again
        client1.strNo = memberViewModel.strNo;
        client1.strname = memberViewModel.strname;
        client1.Suburb = memberViewModel.suburb;
        client1.City = memberViewModel.city;
        client1.Province = memberViewModel.province;
        client1.Country = memberViewModel.country;
        client1.PostalCode = memberViewModel.postalcode;
        client1.zone = memberViewModel.zone;
        client1.flatName = memberViewModel.flatName;
        client1.flatNo = memberViewModel.flatNo;
        client1.IsHa = memberViewModel.isHa;
        client1.AddType = memberViewModel.addtype;
        context.Addresses1.Add(client1);
        contact.Email = memberViewModel.email;
        contact.cell = memberViewModel.cell;
        contact.cell2 = memberViewModel.cell;
        contact.tel_h = memberViewModel.tel_h;
        contact.tel_w = memberViewModel.tel_w;
        contact.fax = memberViewModel.fax;
        contact.m_id = client.m_id;              // NB
       context.Contacts.Add(contact);
        context.SaveChanges();
    }

There is one major issue here. client.m_id = 0. This is because we haven't manually set the value for m_id . Even if m_id is DB generated. The id will only be given to the entity on context.SaveChanges.

To resolve this, if your m_id field is DB generated, you can copy context.SaveChanges(); just under client1.h_ID = head.h_id; then before setting the values for contact , initialize an instance of a Member .

Again, here I think you should add a cell field to Member so you can reference it like this:

Member temp = context.Member.FirstOrDefault(x => x.cell.Equals(memberViewModel.cell));

You can then change this line contact.m_id = client.m_id; to this contact.m_id = temp.m_id ;

Hope this helps you

You have to create ViewModel which will contains Dates what You need from both tables. In Cotroler You crate new Object type Member, and load by LINQ or EF Head ID to Your model.

Member p1=new Member();
p1.Name=ViewModel.Name; etc..

after that, In secound table You add p1.ID in secound table

    context.Add(p1)
    context.saveChanges();
    Head.Id=p1.ID;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM