[英]Passing model from view to controller using Html.RenderAction results in model being null
我有以下模型:
public class Foo
{
public List<Employee> Employees{ get; set; }
public List<Company> Companies{ get; set; }
public List<Admin> Admins{ get; set; }
}
然后我有控制器動作:
public ActionResult Index()
{
Foo model = GetFooFromSomewhere();
return PartialView("Index", model);
}
public ActionResult Employees(List<Employee> model)
{
return PartialView("Employees", model);
}
public ActionResult Companies(List<Company> model)
{
return PartialView("Companies", model);
}
public ActionResult Admins(List<Admin> model)
{
return PartialView("Admins", model);
}
那我有意見
Index.cshml:
@model Foo
@if(Model.Employees.Count > 0)
{
@{Html.RenderAction("Employees", "Config", Model.Employees);}
}
@if(Model.Companies.Count > 0)
{
@{Html.RenderAction("Companies", "Config", Model.Companies);}
}
@if(Model.Admins.Count > 0)
{
@{Html.RenderAction("Admins", "Config", Model.Admins);}
}
Employees.cshtml:
@model List<Employee>
//Display model here
Companies.cshtml
@model List<Company>
//Display model here
Admins.cshtml
@model List<Admin>
//Display model here
如您所見,我使用Index.cshtml來獲取包含多個列表的對象。 這是因為如果列表中未找到任何項目,則需要隱藏操作。 但是,當我再次使用@ Html.RenderAction(...)將它們傳遞給控制器時,當我期望使用List時,在控制器動作內將為null。 為什么?
以這種方式嘗試: 控制器:
public ActionResult Admins(List<Admin> m)
{
return PartialView("Admins", m);
}
視圖:
@{Html.RenderAction("Admins", "Config", new { m = Model.Admins });}
您必須將初始化的模型傳遞給控制器中的Index
視圖。
public ActionResult Index()
{
Foo model = GetFooFromSomewhere();
return PartialView("Index", model);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.