Solved

How do I display information gathered from database in _layout templace?  MVC4

Posted on 2014-01-07
2
220 Views
Last Modified: 2014-01-08
Hi

I would like to pull some information out of the database depending on some information I store to a session.

Using asp we do that in the load event of a page and then assign the result to a asp label.

I am able to do it in the views but I am not sure how to do it in a template.

Any help is always appreciated.
0
Comment
Question by:elmbrook
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39763605
You should not be accessing the database from a view, you should do it from the controller method that renders the view and pass whatever information is needed by the view in a model/viewmodel.

There are a couple of options for getting that information into the layout page.

1. Create a base class viewmodel and have all viewmodels inherit from it. Make the layout page use the base class as its strongly-typed view.

2. Put everything needed by the layout page into ViewData/ViewBag and access it that way from the layout page.

Using #2 it's pretty easy to create an action filter on your base controller class that populates the ViewBag so you're sure it gets done on every action.

P.S. You'll do better with MVC if you stop thinking in terms of WebForms and page events. WebForms and MVC are fundamentally different and trying to look for analogous functionality is just going to hurt you in the long run.
0
 

Author Closing Comment

by:elmbrook
ID: 39766557
Thanks Craig.

This is my first MVC project and you are right about stopping thinking in terms of webforms.
However, I needed to explain how I wanted it to work .

In the end I went with solution 2 which was easy to implement.

Thanks.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question