如何在子部分视图中呈现部分?
How can I render section in a children Partial view?
我使用的runtime&SDK是最新版本.net 6
以下是我项目 _Layout.cshtml 中的三个局部视图:header.cshtml/aside.cshtml/footer.cshtml .
为什么我把它们分成了三个部分,因为它们的代码太多了,为了编码方便,我必须把它们分开。
现在我需要在 header.cshtml 中添加一个部分。这是我的代码:
<header>
///some other codes
@RenderSection("ProductNav", required: false)
</header>
不管有没有ProductNav的部分,程序运行之后页面都是空白的,没有任何错误。
我发现我似乎无法在儿童部分视图中使用 RenderSection
。
似乎还有另一种方法,使用 Html.Partial
来实现。
但是,它需要将我要添加的视图转换为string
。太麻烦了
有没有简单的方法可以做到这一点?谢谢。
你想在部分页面显示导航栏,你使用partial view
来实现这个功能,但是用户不能直接在partial view
中添加css样式。
我的想法是您可以在 wwwroot
中创建一个 External CSS file
并在需要加载的页面中使用它 partial view
.
我写了一个demo来证明这个想法的可行性。
Views/Shared/_header.cshtml
<h2>This is head Partial View H2</h2>
<p>This is head Partial View P</p>
wwwroot/css/header.css
h2 {
color: blue;
}
p {
color: red;
}
views/Shared/_Layout.cshtml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - EmptyNEt5MVc</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
//add the css file in here
@await RenderSectionAsync("Css", required: false)
</head>
//.........
查看
@{
ViewData["Title"] = "Privacy Policy";
}
//use partial view
<partial name="_header" />
<h1>@ViewData["Title"]</h1>
<p1>Use this page to detail your site's privacy policy.</p1>
//reference css file
@section Css{
<link rel="stylesheet" href="~/css/header.css" />
}
然后可以看到`header partial view'加载css文件成功
=============================================
其实View Component
比Partial View
更适合做导航栏,关于View Component
有一个document。
我使用的runtime&SDK是最新版本.net 6
以下是我项目 _Layout.cshtml 中的三个局部视图:header.cshtml/aside.cshtml/footer.cshtml .
为什么我把它们分成了三个部分,因为它们的代码太多了,为了编码方便,我必须把它们分开。
现在我需要在 header.cshtml 中添加一个部分。这是我的代码:
<header>
///some other codes
@RenderSection("ProductNav", required: false)
</header>
不管有没有ProductNav的部分,程序运行之后页面都是空白的,没有任何错误。
我发现我似乎无法在儿童部分视图中使用 RenderSection
。
似乎还有另一种方法,使用 Html.Partial
来实现。
但是,它需要将我要添加的视图转换为string
。太麻烦了
有没有简单的方法可以做到这一点?谢谢。
你想在部分页面显示导航栏,你使用partial view
来实现这个功能,但是用户不能直接在partial view
中添加css样式。
我的想法是您可以在 wwwroot
中创建一个 External CSS file
并在需要加载的页面中使用它 partial view
.
我写了一个demo来证明这个想法的可行性。
Views/Shared/_header.cshtml
<h2>This is head Partial View H2</h2>
<p>This is head Partial View P</p>
wwwroot/css/header.css
h2 {
color: blue;
}
p {
color: red;
}
views/Shared/_Layout.cshtml
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - EmptyNEt5MVc</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
//add the css file in here
@await RenderSectionAsync("Css", required: false)
</head>
//.........
查看
@{
ViewData["Title"] = "Privacy Policy";
}
//use partial view
<partial name="_header" />
<h1>@ViewData["Title"]</h1>
<p1>Use this page to detail your site's privacy policy.</p1>
//reference css file
@section Css{
<link rel="stylesheet" href="~/css/header.css" />
}
然后可以看到`header partial view'加载css文件成功
=============================================
其实View Component
比Partial View
更适合做导航栏,关于View Component
有一个document。