views:

265

answers:

4

My user object has the property: IsAdministrator

Now I want to inject HTML on every page, so in my master page, only if the user is an administrator.

What is the best way to do this?

I was thinking of creating a user control, and then calling RenderPartial.

But I need to access the Request object to see if the user is authenticated, and if they are, to grab the user object from the cache and then check the IsAdminstrator property.

Help?

A: 
  1. You create a basecustomviewmodel with a property called IsAdministrator.
  2. You overload the function OnActionExecuted in a base controller from which the other controller will inherits. Overload that will set IsAdministrator in function of the request.
  3. For each actionresult you pass a customviewmodel inheriting the basecustomviewmodel of 1)
  4. You create a partialview that will be passed in the master that take a basecustomviewmodel as model
  5. enjoy
Gregoire
I actually hav done steps #1-3. How to I create a partialview that will be passed in the master?
Blankman
Go to the views/shared directory. Right click and choose 'Add', then 'View' fill in the options (including 'Create a partial view (.ascx)'. Then use `<% Html.RenderPartial() %>` and point it to your view.
Dan Atkinson
@dan but what about the model I have to pass to the RenderPartial call? How to I get hold of a model form within my master page?
Blankman
Well, the master page shouldn't/can't concern itself with models themselves as it's agnostic to them. Instead you could pass the user in ViewData. To save doing this in every since controller action, you could create a base controller with an override on the OnActionExecuted to add the current user in. The only thing you'll need to pass in with the RenderPartial call is ViewData. This is pretty the process in Gregoire's answer.
Dan Atkinson
@Dan thanks, I just don't see how I can pass anything to my master from within a view that's all. My master is defined in the @Page directive of the view, how can I pass it ViewData?
Blankman
your master page must inherits ViewMaster<yourbasecustomviewmodel>
Gregoire
A: 

Try adding a control on the front end and putting the content you would like to display inside it.

eg:

<asp:Placeholder ID="myPlaceholder" runat="server" Visible="">
   .... HTML for Admins
</asp:Placeholder>

Then on page load in the code behind do something like this -

    if(user.IsAdministrator)
       myPlacholder.Visible = true;
   else
       myPlaceholder.Visible = false;
TGuimond
Wait... Codebehinds? This is ASP.NET MVC. Why on Earth would you want to use codebehinds?! Also, rather than that huge ass if statement, why not do `myPlaceholder.Visible = user.IsAdministrator;`
Dan Atkinson
Oops, sorry didn't realize it was MVC
TGuimond
lol at the huge if and dan's comment :-)
minus4
A: 

i know this is answered and dont want to tread, but i have this kind of situation where if logged in i dont want to show the login on my masterpage.

i have downloaded the futures MVC and implimented RenderAction, this allows me have an action that checks for login, and renders the login view or the custom user view.

simple too

minus4
A: 

Controller:

public class HomeController : BaseController
{
  public ActionResult Index()
  {
    return View();
  }
}

BaseController:

public class BaseController : Controller
{
  public override void OnActionExecuted(ActionExecutedContext context)
  {
    base.OnActionExecuted(context);

    bool isAdministrator = context.HttpContext.Request.IsAuthenticated && context.HttpContext.User.IsInRole("Administrator");
    context.Controller.ViewData.Add("IsAdministrator", isAdministrator);
  }
}

Master page:

<%
  bool isAdministrator = bool.Parse(ViewData["IsAdministrator"].ToString());
  if(isAdministrator) {
    Html.RenderPartial("Users/UserControl");
} %>

~/Views/Shared/Users/UserControl.ascx

Access the current administrator here with <%= Page.User %>.

Dan Atkinson