user3351001
user3351001

Reputation: 11

Mvc Layout page dynamic content

I have an MVC layout page(master page) with some dynamic content in the header and footer.

       <header>

        @Html.Action("GetHeader", "Layout")

    </header>

   <div id="body">

           @RenderBody()

    </div>

    <footer>
        @Html.Action("GetFooter", "Layout")
    </footer>

GetHeader and GetFooter action methods get values from database into an object and return partial view based on the header/footer properties of that object.

But the thing is I do not want to make two calls to the database,because they both use the same object type.

so is there any way so that I can call database just once and retain that object and use it through out the layout page,without calling the database multiple times?

any solution other than using viewbag will be very helpful.

Thanks!

Upvotes: 1

Views: 7532

Answers (4)

Roland Schaer
Roland Schaer

Reputation: 1668

I think a good approach would be to render the sections as part of your view output using section definitions and then add them to the layout using the "@RenderSection" method.

<header>
   @RenderSection("AdditionalHeader", required: false)
</header>
<div id="body">
   @RenderBody()
</div>
<footer>
   @RenderSection(("AdditionalFooter", "Layout")
</footer>

Then in your view create the sections to be rendered:

@Section AdditionalHeader
{
   <whatever content you want to generate from action>
}
@Section AdditionalFooter
{
   <whatever content you want to generate from action>
}

This should work with MVC 3 and forward. Scott Guthrie did a more detailed write-up if you need more details:

http://weblogs.asp.net/scottgu/asp-net-mvc-3-layouts-and-sections-with-razor

Upvotes: 1

Chris Perry
Chris Perry

Reputation: 121

Cache the value in a static property with a static private backing field. In the getter check if the backing field is null. If it is null, then query the database and set it. Have the property return the backing field.

This should be in your data access layer. This is called the singleton pattern.

Upvotes: 1

r.vengadesh
r.vengadesh

Reputation: 1837

Use this way Create a header.cshtml for header and footer.cshtml for footer in shared folder

Then call this .cshtml page into layout page using @{Html.RenderPartial("header");} and @{Html.RenderPartial("footer");}

In the header.cshtml and footer.cshtml page you put dynamic values using sql query or linq query

Upvotes: 0

Kira
Kira

Reputation: 1443

Modified Answer:

You can use the ViewBag, ViewData, etc., available in MVC, to store the data from your database and use it throughout your MVC application without having to call them again.

This Link explains about them, you can use it based on your requirement.

All you have to do is store data from database in your main action method (Index by default),

ViewData["Data"]=data;

data can be an object of any type that holds the value from database.

In your view page you can pass it to other action methods like

@Html.Action("ActionMethod", (ObjectTypeOfData)ViewData["Data"])

In your action method you can get it as a parameter like,

public ActionResult ActionMethod(ObjectTypeOfData data){ .... your code ....}

Note: If you do not want to retrieve data from your database during post back use Session instead of ViewData

Upvotes: 0

Related Questions