Solved

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

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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 …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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