我的项目ASP.NET MVC 5在CREATE ACTION上有问题。这是我的代码:
模型
成员:
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();
}
发布行动:
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。“
答案 0 :(得分:3)
根据我们的评论,在您的Post
操作中,我建议您使用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
是生成数据库。 id只会被赋予context.SaveChanges上的实体。
要解决此问题,如果您的m_id
字段是生成数据库的,那么您可以在context.SaveChanges();
下方复制client1.h_ID = head.h_id;
,然后在设置contact
的值之前,初始化一个实例Member
。
同样,在这里,我认为您应该向cell
添加Member
字段,以便您可以像这样引用它:
Member temp = context.Member.FirstOrDefault(x => x.cell.Equals(memberViewModel.cell));
然后,您可以将此行contact.m_id = client.m_id;
更改为此contact.m_id = temp.m_id
;
希望这有助于你
答案 1 :(得分:0)
您必须创建ViewModel,它将包含两个表所需的日期。 在Cotroler你创建新的Object类型成员,并通过LINQ或EF头ID加载到您的模型。
Member p1=new Member();
p1.Name=ViewModel.Name; etc..
之后,
在secound表中,您可以在secound表中添加p1.ID
context.Add(p1)
context.saveChanges();
Head.Id=p1.ID;