Implementing logic to provide role based viewdata

nielz_01

New Member
I have been thinking about this for the past few days and was wondering what everyone else's take is on it. I want to implement some content that is based on what role the user belongs to. Our applications uses HTTP headers to receive role information. In MVC should I have the controller handle the role detection, and then pass a boolean value into the viewmodel.Viewmodel:\[code\]public class ProductViewModel{ public Product MyProduct { get; set; } public bool IsAdmin { get; set; } public bool IsSuperAdmin { get; set; }}\[/code\]Controller:\[code\]public class ProductController : Controller{ public ActionResult Info() { //get product information here ProductViewModel pvm = new pvm(); pvm.Product = theProduct; pvm.IsAdmin = checkAdminAccess(); //This would return a bool if they had access pvm.IsSuperAdmin = checkSuperAdminAccess();//This would return a bool if they had access return View(pvm); }}\[/code\]View:\[code\]@if(Model.IsAdmin == true || Model.IsSuperAdmin == true){ //Render Content for admin (Either directly or via Partial View)}@if(Model.IsSuperAdmin == true){ //Render Superadmin content (Either directly or via Partial View)}//Implement other Product Stuff Here\[/code\]Or would it be better to create a partial view and handle the logic in the view. This seems like there is less work involved.\[code\]@if (Request.Headers.AllKeys.Contains("role")){ ViewBag.Role = Request.Headers["role"].ToString();}...@if(ViewBag.Role == "Admin" || ViewBag.Role == "SuperAdmin"){ @Html.Partial("AdminPartial")//Or Render Code Directly? if(ViewBag.Role == "SuperAdmin") { @Html.Partial("SuperAdminPartial")//Or Render Code Directly? }}//Implement other Product Stuff Here\[/code\]Is there any added benefits to doing it one way or another, should I be rendering the content directly or using a partial view or does it not matter? Or am I missing some other way that this should be done?
 
Back
Top