我不得不模型,说A和B妥善处理模型视图响应MVC中的ASP.NET EF
public class A
{
public int AID {get; set; }
public string Name {get; set;}
public virtual B B {get; set;}
}
public class A
{
[ForeignKey("A")]
public int BID {get; set; }
public string Name {get; set;}
public virtual A A {get; set;}
}
而且一个ViewModel
public class ABViewModel
{
public A A {get;set;}
public B B {get;set;}
}
然后,我希望有一个视图,可以让我设定两个模型的名称同时,所以控制器看起来像
public ActionResult Create()
{
var viewModel = new ABViewModel();
return View(viewModel);
}
到目前为止好,响应如何?类似以下内容返回错误
值不能为空。参数名:实体
public ActionResult Create([Bind(Include = "A, B")] ABViewModel stuff)
{
if (ModelState.IsValid)
{
db.A.Add(stuff.A); // <-- here occurs the error
stuff.B.A = stuff.A;
db.B.Add(stuff.B);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(stuff);
}
'“...返回一个错误”' - – David
你需要包括你得到的例外。 – ngeksyo
更新了问题 – rst