Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I have a pretty common issue: i need to know the name of current user and also name of the current controller so i can highlight/disable some links.

Here's some solutions i found by myself:

1: Get all what's needed inside PartialView.

So i have a PartialView inside my page layout, that takes user name like this:

@{ var userName = Context.ToContext().GetUser<Agent>().Name; }

And here's the way i take controller name:

@{ var controllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString(); }

I have some problem with this code, for it is suspected to ruin MVC pattern. So i have another solution, which i consider even more stupid.

2: I've created new controller called like "ContextController", here's the code:

public class ContextController : BaseController // BaseController is just a facade
{
    public string GetUserName() {
        return Context.GetUser<Agent>().Name;
    }
}

Here's the problem: i cannot get controller name with this way, cuz it will always be like "Context", which is useless for me. And i also get the name on view like this:

@{ var userName = Html.Action("GetUserName", "Context"); }

3: The last way i figured out is to pass needed strings through ViewData. But i have about 18 controllers and some of 'em have like 3 methods, returning ViewResult. It's not bad design, it's just reality. And i don't actually want to create and pass ViewDataDictionary for every single method, that returns ViewResult. Maybe i could extend my BaseController, so ViewDataDictionary for every page will always have userName and controllerName? But here comes another problem: i have whole lot of AJAX in my project, and it seems to me not legit enough to pass unused data every time controller action is called.

If you know better solution, i'd be happy to hear it. Thanks in advance.

UPD: Well, i've found the solution, that worked for me: I've created an abstract class LayoutViewModel:

public abstract class LayoutViewModel
{
    public string UserName { get; set; }
    public string UserCode { get; set; }
    public ActiveLinkEnum ActiveLink { get; set; }
}

Every ViewModel in my project is now an inherit class for LayoutViewModel. I also extended BaseController class.

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    base.OnActionExecuted(filterContext);

    var viewResult = (ViewResult) filterContext.Result;
    var layoutViewModel = (LayoutViewModel) viewResult.Model;
    var agent = Context.GetUser<Agent>();
    layoutViewModel.UserName = agent.Name;
    layoutViewModel.UserCode = agent.Code;
    layoutViewModel.ActiveLink = activeLinkResolver.Resolve(filterContext.RouteData.Values["controller"].ToString());
}

So it allowed me: 1. To incapsulate active link determination logic inside builder (IActiveLinkResolver). 2. To easily extend common data on every page. Now my Model doesn't depends on View at all. 3. To evade using of ViewData, which is not an option, due to pt.2.

Thanks to everyone.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

You should try creating ViewModel classes - they're simple classes which just have a bunch of properties, and you pass them to the view instead of using ViewData or ViewBag. You can make them all inherit off of a base model which has a bunch of show/hide flags (like user1614543 suggested) or put the flags on individual models (the better choice) which get returned to the views that use those flags. Either way, you can put in sane default values so you only need to set them when you're displaying different data. You can even pass the name of the controller as a string, or use an enum to categorize them.

This will make your markup more readable, and ensure type-safety of your data, which will make working with it easier.

Here's an example, although you'll have to convert it from ASPX to Razor.

share|improve this answer

I think it is controller's responsibility to decide what should be visible to user. So it looks to me like a good idea to pass some boolean flags to ViewData dictionary like

VeiwData["IsSomeUserSpecificLinkVisible"] = Context.GetUser<Agent>().Name == "Foo";

And view should just bind those flags to appropriate attributes of controls.

share|improve this answer
    
So you want me to write this line for every ViewResult i return? It's like in 22 places arount the project. And what if some day i will also need that Agent Email or Id? That's not very adaptive solution, IMO. –  Vladislav Qulin Nov 14 '12 at 14:32
1  
You put behaviour like this in a filter and apply it manually to some actions/controllers or globally to all controllers. –  Kristof Claes Nov 14 '12 at 14:42
    
@KristofClaes What kind of filter? Can u plz show some code sample? –  Vladislav Qulin Nov 14 '12 at 15:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.