我想从一个表保存一个ID到另一个表使用asp.net mvc的5 [英] I want to save an ID from a table to another table using asp.net mvc 5

查看:186
本文介绍了我想从一个表保存一个ID到另一个表使用asp.net mvc的5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目上创建操作ASP.NET MVC 5有问题。这里是我的code:

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

模式

成员:

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

}

视图模型

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

POST操作:

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

我的问题是,我想,以节省成员表的新成员,但新的成员必须保存在头表头ID可以请你协助。在细节结果
H_id是头表的外键的成员表。头部已经得救了,现在我想保存成员表的新成员已经在头表中存在头下。但成员表我要救那个特定的头H_id。

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."

推荐答案

这是我们的看法,在你的发表的行动我会建议使用绑定对于即将所有数据被张贴到数据库。你只需要一提的字段名(为例子,我只提到3场)。

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

这里有一个主要的问题。 client.m_id = 0,这是因为我们还没有手动设置为 m_id 的值。即使 m_id 是DB产生的。该ID将只提供给实体上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.

要解决这个问题,如果你的 m_id 字段是DB产生的,可以复制 context.SaveChanges(); 刚下 client1.h_ID = head.h_id; 联系人设置值之前,初始化的实例成员

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

您可以接着修改这一行 contact.m_id = client.m_id; 这个 contact.m_id = temp.m_id ;

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

希望这有助于你

这篇关于我想从一个表保存一个ID到另一个表使用asp.net mvc的5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆