On my website, I have a section (a floating sidebar) that I want rendered only for a sub-set of users (admins). I'm hoping that I can put the logic in the master layout for determining if the section should be shown or not but that causes an error on the page if the section isn't rendered.
在我的网站上,我有一个部分(一个浮动的边栏),我只希望为一组用户(管理员)呈现。我希望我可以将逻辑放在主布局中,以确定是否应该显示section,但是如果没有呈现该部分,就会导致页面上出现错误。
Example code - Layout.cshtml...
示例代码- Layout.cshtml…
... code ...
@if(user.IsAdmin) {
@RenderSection("AdminSidebar", false)
}
Example code - MyPage.cshtml...
示例代码- MyPage.cshtml…
@section AdminSidebar {
... code ...
}
Does anybody know how to get this to work without having to put the logic in all of the child pages?
有没有人知道如何在不需要将逻辑放入所有子页面的情况下使其工作?
As a note, IsSectionDefined("AdminSidebar") only works in the layout file. It doesn't work in the page to test if the section is available or not.
注意,IsSectionDefined(“admin边栏”)只在布局文件中工作。在页面中无法测试该部分是否可用。
3 个解决方案
#1
16
I don't know if this is not abusing the framework, but if you're really inclined to go that way you could try the following:
我不知道这是不是滥用框架,但如果你真的倾向于这样做,你可以试试下面的方法:
@{
if(user.IsAdmin) {
@RenderSection("AdminSidebar", false)
}
else {
RenderSection("AdminSidebar", false).WriteTo(TextWriter.Null);
}
}
#2
2
In my _Layout.cshtml file I did something like this:
在我_Layout。cshtml文件
@if(user.IsAdmin)
{
@Html.Partial("SideBar")
}
to avoid having to have all of the child pages have to deal with the optional section in essentially the same manner. When I first tried the optional section thing, I found that I was repeating myself in the child pages, at least in my implementation.
为了避免必须拥有所有子页面,必须以基本相同的方式处理可选部分。当我第一次尝试可选部分时,我发现我在子页面中重复自己,至少在实现中是这样。
Where I've used the @RenderSection
call for optional sections, it has generally been to provide page-specific stuff.
在使用@RenderSection调用可选部分时,通常都是提供页面特定的内容。
#3
0
Using a section for something that is conditional based on the users permission level feels a little dirty to me. I would use RenderPartial(user) and put the logic in the partial.
对于基于用户权限级别的有条件的东西,使用部分会让我觉得有点脏。我将使用RenderPartial(user)并将逻辑放在partial中。
@if(user.IsAdmin) {
..code..
}
#1
16
I don't know if this is not abusing the framework, but if you're really inclined to go that way you could try the following:
我不知道这是不是滥用框架,但如果你真的倾向于这样做,你可以试试下面的方法:
@{
if(user.IsAdmin) {
@RenderSection("AdminSidebar", false)
}
else {
RenderSection("AdminSidebar", false).WriteTo(TextWriter.Null);
}
}
#2
2
In my _Layout.cshtml file I did something like this:
在我_Layout。cshtml文件
@if(user.IsAdmin)
{
@Html.Partial("SideBar")
}
to avoid having to have all of the child pages have to deal with the optional section in essentially the same manner. When I first tried the optional section thing, I found that I was repeating myself in the child pages, at least in my implementation.
为了避免必须拥有所有子页面,必须以基本相同的方式处理可选部分。当我第一次尝试可选部分时,我发现我在子页面中重复自己,至少在实现中是这样。
Where I've used the @RenderSection
call for optional sections, it has generally been to provide page-specific stuff.
在使用@RenderSection调用可选部分时,通常都是提供页面特定的内容。
#3
0
Using a section for something that is conditional based on the users permission level feels a little dirty to me. I would use RenderPartial(user) and put the logic in the partial.
对于基于用户权限级别的有条件的东西,使用部分会让我觉得有点脏。我将使用RenderPartial(user)并将逻辑放在partial中。
@if(user.IsAdmin) {
..code..
}