3

I'm working on an MVC application that will be used by some internal (that is, Windows authenticated) employees. Unfortunately for me, our Active Directory accounts aren't aware of each employee's ID -- our employee database doesn't talk to AD at all (cringe-inducing, I know).

Anyway, the website must record the each employee's ID as he or she makes changes to various records. In the past (in ASP.NET WebForms), I would've added a reference to a custom class in the Session and just used that to check for access, grab the employee's ID, and display the employee's full name at the top of the application.

How would I do this in MVC? I know that I can add data to the Session, but I can't find any way to cleanly reference Session data in the _Layout.cshtml file, and I would honestly prefer to store this data in a class that I can get Intellisense with, rather than using an array indexer. Am I being too picky?

To be clear: I'm trying to figure out how to reference information stored in a custom class in a layout or master page, or if that's just a completely stupid idea.

4
  • ViewBag could be used to pass data to view , or you can just as easily access the session variables right from razor code in view Commented Apr 10, 2013 at 21:44
  • I guess I am having trouble figuring out exactly what you are asking. Do you need to know Razor syntax for importing a namespace into the page, or do you need to know how Principals and Identities are managed in a modern ASP framework application? Commented Apr 10, 2013 at 21:45
  • @DavidC, I guess it'd be the latter. I know how to import namespaces, but part of our template requires that we identify logged in users on our master (or _Layout) pages, and my boss would prefer that this not be their login ID. Commented Apr 10, 2013 at 21:47
  • Updated my question to clarify that point. Apologies for the confusion. Commented Apr 10, 2013 at 21:49

3 Answers 3

3

As Christopher said, a strongly typed view is the best approach from an enterprise design standpoint. However, the fact that it is in your layout is what is confounding you. What you need to do is inside your Layout, make a call to a controller action that will render the content you want, and THAT view it returns, is strongly typed to a view model that contains all the data you would need to display.

Lets say you want to write out a user menu, with some options, depending upon their security access, and a "Welcome Bob Ross" message (assuming Bob is logged in).

So in your view your _Layout you would have a div like...

        <div id="header" class="header">
            @{ Html.RenderAction("RenderUserMenu", "Home"); }
        </div>

Now, in the Home controller, you have an action named RenderUserMenu that looks kinda like

public ActionResult RenderUserMenu()
        {
            var currentUser = DataManager.GetCurrentUser();
            return PartialView("_UserMenu", currentUser);
        }

This menu is strongly typed to a Domain User object, so inside that view, you can now print out whatever information about a user you want, and it's part of the _Layout and shows up on every page that inherits from it.

The only voodoo here is the DataManager.GetCurrentUser() function, but I am hoping you have some type of infrastructure to get the current user anywhere in a controller. If not, its simply a function that pulls the user key from the session, queries the database, and populates a domain object with the users data. If you are using an ORM like Entity Framework or NHibernate, its even easier.

Sign up to request clarification or add additional context in comments.

2 Comments

That's exactly what I ended up doing. I'm sticking closely to the standard MVC layout for the time being (until I can rewrite our internal css stylesheet for the mobile-aware world), so I lifted their <section> and <nav> tags for my partial view. In this case, the application's also really simple, so it has a whopping two links in the menu bar for admin users.
This is another way I've accomplished this in the past. The only thing I didn't like about this, is that it instantiates another controller and fires of another method. From there, it was easy to keep adding new methods to be rendered, and we ended up with quite a few of them. I just didn't like the way it turned out (nothing particularly wrong with it), which is why I added a different suggestion below. If at all possible, I would have liked to keep it all in the layout, since most of it wasn't going to be reused anyways, and it's easier (for me) to grok that way.
2

If you need to pass around the user information in its own model/class, this is similar to this question/answer

Getting data into a partial view or layout using MVC4

1 Comment

Yep. That looks like more or less what I was thinking about -- I hadn't considered partial views at all, so I was barking up the wrong tree entirely. Thanks!
1

You probably want to use a strongly typed view.

http://www.asp.net/mvc/tutorials/views/dynamic-v-strongly-typed-views

You can change the base view type as described in this article by Phil Haack.

http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx

3 Comments

I'm familiar with strongly-typed views -- I'm asking about how I'd use this in a layout or master page.
Ahh, yes! You can modify the web.config to override the view base. I'll get back to you on how to do this.
so basically, you should be able to put whatever you needed to into that base view... or find some way of injected what you needed to into that base view. I think MVC 4 has some dependency injection you can hook into, or you could use another dependency injection framework (such as autofac) to inject the things you need into the view.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.