我想使用asp.net mvc 5将ID从一个表保存到另一个表

本文关键字:一个 保存 另一个 ID asp net mvc | 更新日期: 2023-09-27 18:28:45

我在CREATE ACTION上的项目ASP.NET MVC 5中遇到问题。这是我的代码:

型号

成员:

  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; }
}

}

磁头:

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; }
}

}

控制器

获取操作:

  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();
    }

行动后:

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();
        }

我的问题是,我想在成员表上保存一个新成员,但新成员必须从heads表中保存head ID,你能帮忙吗。详细信息"
H_id是头表中成员表的外键。head已经保存,现在我想在heads表中已经存在的head下的members表中保存一个新成员。但在成员表上,我想保存那个特定头的H_ id。"

我想使用asp.net mvc 5将ID从一个表保存到另一个表

根据我们的评论,在您的Post操作中,我建议对将发布到DB的所有数据使用Bind。您只需要提到字段名称(对于我只提到3个字段的示例)。

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();
    }

这里有一个主要问题。client.m_id=0。这是因为我们没有手动设置m_id的值。即使m_id是DB生成的。id将仅在上下文中提供给实体。保存更改。

要解决此问题,如果m_id字段是DB生成的,则可以将context.SaveChanges();复制到client1.h_ID = head.h_id;下,然后在设置contact的值之前,初始化Member的实例。

同样,在这里,我认为您应该向Member添加一个cell字段,这样您就可以这样引用它:

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

然后,您可以将此行contact.m_id = client.m_id;更改为此行contact.m_id = temp.m_id

希望这能帮助您

您必须创建ViewModel,它将包含两个表所需的日期。在Cotroler中,您装入新的Object类型Member,并通过LINQ或EF Head ID加载到您的模型中。

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

之后,在secound表中您将p1.ID添加到secound表格中

    context.Add(p1)
    context.saveChanges();
    Head.Id=p1.ID;
相关文章: