c# - 如何将一个部分传递给部分布局?

标签 c# asp.net asp.net-mvc asp.net-core asp.net-mvc-5

在 ASP.net MVC 5 中,我们可以使用

将部分传递给布局
@section AnySection{
  //section code here
}

并在 Layout by 中渲染它

@RenderSection("AnySection", required: false)

但是我们如何才能将该部分再次传递给该布局中的部分内容呢?请参阅下图以供引用 how can we pass that section to partial? subheader-v1 是我的布局中的局部 View 我用许多局部 View 制作了我的布局。 当我按照上面的图片中提到的那样尝试时,它给了我这个错误

The file "~/Views/Shared/partials/_subheader/subheader-v1.cshtml" cannot be requested directly because it calls the "RenderSection" method.'

最佳答案

您没有将部分传递给布局。布局决定了哪些部分应该(或可以)在 View 中呈现……它还决定了该部分应该呈现在 View 中的哪个位置。

来自 MS Documentation :

A layout can optionally reference one or more sections, by calling RenderSection. Sections provide a way to organize where certain page elements should be placed.

部分在部分 View 中不起作用,这是设计使然。您需要将 RenderSection 移动到您的布局,并将部分主体移动到您的 View 。参见 this question获取更多信息。

关于c# - 如何将一个部分传递给部分布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61052525/

相关文章:

c# - 重复执行一段代码而没有超时。窗口服务

C#,我可以将字典初始代码从构造函数中移出吗?

c# - 使用多个表中的数据创建下拉列表

c# - .NET 的汇编程序库,将运行时变量字符串组装成机器代码以进行注入(inject)

c# - 为什么我预期的空对象不为空?

c# - 强类型绑定(bind)到 DropDownListFor?

c# - HttpHandler 被忽略以支持路由

asp.net-mvc - asp.net mvc 大文件上传

c# - AngularJS:如何使用 AngularJS 而不是 Razor 设置 MVC 模型数据值?

asp.net-mvc - 动态调查表的MVC模型