[英]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.