c# - 使用 Html.RenderAction 将模型从 View 传递到 Controller 会导致模型为空

标签 c# asp.net-mvc renderaction

我有以下模型:

public class Foo
{
    public List<Employee> Employees{ get; set; }
    public List<Company> Companies{ get; set; }
    public List<Admin> Admins{ get; set; }
}

然后我有我的 Controller Action :

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);
}

那我有我的看法

索引.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);}
}

员工.cshtml:

@model List<Employee>

//Display model here

公司.cshtml

@model List<Company>

//Display model here

Admins.cshtml

@model List<Admin>

//Display model here

如您所见,我使用 Index.cshtml 获取包含多个列表的对象。这是因为如果在列表中找不到任何项目,我需要隐藏操作。但是,当我再次使用@Html.RenderAction(...) 将它们传递给 Controller ​​时,当我期待一个列表时,我在 Controller 操作中得到了空值。为什么?

最佳答案

这样试试: Controller :

public ActionResult Admins(List<Admin> m)
{
    return PartialView("Admins", m);
}

查看:

@{Html.RenderAction("Admins", "Config", new { m = Model.Admins });}

关于c# - 使用 Html.RenderAction 将模型从 View 传递到 Controller 会导致模型为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38310803/

相关文章:

javascript - 如何在 MVC 5 Razor 中调用和刷新局部 View ?

c# - 创建 Controller 基类(部分)

asp.net-mvc - Html.RenderAction 是否创建单独的请求?

c# - 使用 Process.Start() 启动时安装程序失败,但双击时可以运行

c# - 在 Unity 中运行时将带有文本的按钮动态添加到 UI

c# - 在 MVC 中将文本与图像居中

asp.net-mvc - ASP.Net MVC 可重用形式作为 RenderAction 或 RenderPartial

c# - 去除不在安全列表中的 HTML 标签的方法

c# - 将 Sprite x,y, width, height 转换为视口(viewport)矩形

asp.net-mvc - ASP.NET MVC - 从 URL 中删除 Controller 名称