Solved

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

Posted on 2014-01-07
2
210 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now