Created
August 23, 2018 22:42
-
-
Save brynner/7de01fb7446bddd9116847323d18b10d to your computer and use it in GitHub Desktop.
C# - Rendering different Partials and Scripts according to a statement using Razor.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'm an admin. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Mvc; | |
namespace ProjectName.Pages.Report.Controllers | |
{ | |
[Page("Report")] | |
public class ReportController : Controller | |
{ | |
public IActionResult Index() | |
{ | |
if (Request.Cookies["role"] != null) { | |
ViewData["CookieRole"] = Request.Cookies["role"].ToLower(); | |
} | |
return View(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@{ | |
Layout = "Layout.cshtml"; | |
} | |
@{ | |
var cookieRole = ViewData["CookieRole"]; | |
@switch (cookieRole) { | |
case "admin": | |
@Html.Partial("Admin.cshtml") | |
@section Scripts { | |
<script>console.log('admin scrips');</script> | |
} | |
break; | |
case "user": | |
@Html.Partial("User.cshtml") | |
@section Scripts { | |
<script>console.log('user scripts');</script> | |
} | |
break; | |
default : @Html.Raw("There is no role.") | |
break; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RenderSection("Scripts", required: false) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'm an user. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment