Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

705 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

13 Experts available now in Live!

Get 1:1 Help Now