Skip to content

Instantly share code, notes, and snippets.

@brynner
Created August 23, 2018 22:42
Show Gist options
  • Save brynner/7de01fb7446bddd9116847323d18b10d to your computer and use it in GitHub Desktop.
Save brynner/7de01fb7446bddd9116847323d18b10d to your computer and use it in GitHub Desktop.
C# - Rendering different Partials and Scripts according to a statement using Razor.
I'm an admin.
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();
}
}
}
@{
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;
}
}
@RenderSection("Scripts", required: false)
I'm an user.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment