我的模型中有多个其他模型。
public class mainmodel
{
public entity1 ();
public entity2 ();
}
发布视图后,我将entity1实体设为null?运气好的话。我做错了? 我的问题是:Asp.net with MVC multiple model in one view (create, update) 如何在模型中获取这些类实体?
答案 0 :(得分:0)
Controller(HomeController):
public ActionResult Index()
{
mainmodel model=new mainmodel();
return View(model);
}
[HttpPost]
public ActionResult Index(mainmodel model)
{
return View(model);
}
查看(主页/索引):
@model mainmodel
@using (Html.BeginForm("Index","Home",FormMethod.Post))
{
<input type="submit" value="Submit" />
}
答案 1 :(得分:0)
ViewModel(IndexViewModel):
public class IndexViewModel
{
public mainmodel model1 {get;set;}
public anotherclass model2 {get;set;}
}
Controller(HomeController):
public ActionResult Index()
{
IndexViewModel model=new IndexViewModel();
return View(model);
}
[HttpPost]
public ActionResult Index(IndexViewModel model)
{
return View(model);
}
查看(主页/索引):
@model IndexViewModel
@using (Html.BeginForm("Index","Home",FormMethod.Post))
{
<input type="submit" value="Submit" />
}
现在你可以在HttpPost中获得所有价值。