Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching partial views in asp.net MVC

I have the following situation on a webapp:

A table "Employees" contains column "Department" and "Function". Both are dropdownlists.

The "Function" dropdownlist options depend on the selected "department". (so each department has its own list of functions)

When changing the department, I do an ajax call to a controller action with parameter "DepartmentId". Theres an [outputcache] attribute on the controlleraction so the functions that it returns get cached for every department ID.

My problem is the initial loading of the page. Can you call a controlleraction in a view and take advantage of the caching?

Anyone? 30 views and no answers.. Any remarks about my question? Too obvious? too hard? too weird? something for google (altho I didn't find a solution there) ?

like image 419
Thomas Stock Avatar asked Jun 04 '09 14:06

Thomas Stock


People also ask

What is partial caching in asp net?

Fragment caching does not actually cache a Web Form's code fragments directly; fragment caching refers to the caching of individual user controls (. ascx) within a Web Form. Each user control can have independent cache durations and implementations of how the caching behavior is to be applied.

What is another name of partial page caching in asp net?

Fragment Caching (Partial Page Caching) - Inside ASP.NET [Book]

What are the different caching techniques available in .NET MVC?

Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser. Client- Content is cached on the Web Browser. Server- Content is cached on the Web Server. ServerAndClient- Content is cached on the Web Server and the Web Browser.

How we can call partial view in MVC?

To create a partial view, right click on the Shared folder -> click Add -> click View.. to open the Add View popup, as shown below. You can create a partial view in any View folder. However, it is recommended to create all your partial views in the Shared folder so that they can be used in multiple views.


2 Answers

Phil Haack wrote a short blog post on a similar topic called Donut Hole Caching. It serve as a good starting point.

like image 134
Rick Avatar answered Nov 12 '22 23:11

Rick


I would use subcontrollers or better still partial requests to do what you are asking. In a typical page I tend to not cache the whole page but instead break up areas into different action methods which are called via partial requests. That way I can have output caching on each area with differing expirations. It's more page life-cycles but when they are cached they really are not a tax on performance. It's also far easier to maintain and optimize a specific area if it starts to under perform.

In my experience this also fits very nicely with ajax patterns as you only every "get" your data from one action method.

Partial requests are discussed here and subcontrollers here

Hope this helps.

like image 21
madcapnmckay Avatar answered Nov 12 '22 21:11

madcapnmckay