Josh
Josh

Reputation: 23

Persistent data across internal pipelines / requests

Basically I have a webpage structure where common parts of the page (header, side bar, etc) are generated in a separate controller filled with child actions outputting partial views. I then call these actions (using RenderAction()) from the layout page of the website.

So (if I'm right in saying this), There are multiple internal mvc pipelines (header/sidebar internal requests) including the original request pipeline to for the specific webpage. How/Where can I initialize some data from the original pipeline request and have that data accessible from the other internal mvc pipeline requests?

Summary of what I want to accomplish (with example)

  1. Request for website comes in.
  2. MVC starts pipeline for "Home" controller, "index" action.
  3. Before the Action gets executed, some data needs to be created that can later be accessible.
  4. From the layout page, several "RenderAction" Methods get executed creating sub pipelines for interal requests (e.g. "Shell" controller, "DisplayHeaderBar" action
  5. "DisplayHeaderBar" needs to access some data that was set in step 3 before rendering partial view

Hopefully this makes sense...

Upvotes: 0

Views: 341

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239290

What you're looking for are child actions. You simply create an action in some controller that returns a partial view. For example, you could handle your site navigation via:

[ChildActionOnly]
public ActionResult SiteNavigation()
{
    // get the data for your nav
    return PartialView("_SiteNavigation", yourSiteNavModel);
}

The ChildActionOnly attribute ensures that this action can only be called as a child action, making it inaccessible via typing a URL in a browser's navigation bar.

Then, you create a view in Views\Shared\_SiteNavigation.cshtml:

@model Namespace.To.ClassForSiteNavigation

<!-- render your site navigation using the model -->

Finally, in your layout:

@Html.Action("SiteNavigation", "ControllerWhereThisExists")

Upvotes: 0

okisinch
okisinch

Reputation: 96

I think you could use Tempdata for that. Tempdate gets deleted after you access it, so if you want to use the data more then once use Tempdata.Peek or Tempdata.Keep.

Here is a link with some explanation how you can pass data in asp.net mvc.

https://msdn.microsoft.com/en-us/library/dd394711%28v=vs.100%29.aspx

If tempdata doesn't do it then you could use cache.

Upvotes: -2

Related Questions