Question

From what I can tell, MVC seems to treat partial views differently than the main views. It seems as though I can access an object within the main view, but not its child views if that object is going to be disposed once the main controller action returns (see my question here).

I would assume that this is to facilitate async updating of those partial views via AJAX or some other method long after an object in the main controller action is disposed. This makes perfect sense to me.

The way I'm trying to use a partial view right now, I don't need this async updating capability. I COULD just cut out the partial view all together and put it all together, but what I'm looking for is just a way to segment out my views into smaller chunks, rather than having them all in one huge file.

Ideally, I'd like to be able to pass an object from my controller to my view, utilize that object in the view AND any child/sub views, and dispose of the object only after the view and its sub/child views are finished using it.

EDIT - Adding an example

I have a Big Complicated View in a single file. Imagine that each line is actually a larger, more complicated block of view markup and code:

ViewStuff.cshtml

ViewStuff
ViewStuff
MoreViewStuff
MoreViewStuff
MoreViewStuff
ViewStuff
EvenMoreViewStuff
EvenMoreViewStuff
EvenMoreViewStuff
ViewStuff

I would like to break this view into some separate files for convenience and maintenance, effectively extracting portions of it to sub views, like an "extract method" refactoring sort of:

ViewStuff.cshtml

ViewStuff
ViewStuff
RenderPartialViewOf MoreViewStuff
ViewStuff
RenderPartialViewOf EvenMoreViewStuff
ViewStuff

MoreViewStuff.cshtml

MoreViewStuff
MoreViewStuff
MoreViewStuff

EvenMoreViewStuff.cshtml

EvenMoreViewStuff
EvenMoreViewStuff
EvenMoreViewStuff

From what I can tell, partial views seem to be the answer to this, although there seem to be a lot of methods one could use. The problem I'm getting is that my controller is passing in an object to the view which is disposed when the controller action returns.

In the "all in one file" scenario, I can successfully utilize the object to construct the full view, because the whole view is constructed before the method returns and the object is disposed. However, this is not ideal because it means large, long, monolithic view files and potential duplication of portions later on.

In the "multiple files" scenario, the main view can access the object fine, because it hasn't been disposed yet. But the main controller action method returns, disposing the object, before the partial views have gotten it. The result is an error in the partial view, looking for an object which is no longer available.

What I want is a way to construct a complete view made from multiple organized chunks, all before the controller method disposes of my object.

Again, see my other question for some more specific code regarding what I'm doing with this object, the using statement it's in within the controller, etc.

Was it helpful?

Solution

Your problem has nothing to do with Partials. I've already addressed the issue in your other question. The problem also has nothing to do with async, as it's been this way since the very first version of MVC, which did not support async.

You have to pass objects to the view which are not disposed when the method returns.

OTHER TIPS

It seems you're just confused about the difference between partials and child actions.

A partial view is some HTML without a layout. It will inherit the model of it's parent view, unless you pass a different model. This is most likely what you need to use:

@Html.Partial("_MyPartialView")

That's literally all you need. You don't need to add anything at all to your controller.

Now, child actions are different. They're like normal actions, but run within the main request. In other words, they're not accessed through a URL like a normal action would be, but they are nonetheless, completely separate from the main request. They also function as GETs. You don't POST to a child action, which means you can't send along complex data types like an entire model. Their purpose is to allow you render something that doesn't really fit in with the scope of the rest of the page. For example, if your main action and view are tasked with fetching an rendering a single blog post, you might still want to have a side bar that shows other recent posts. With a child action you can fetch the list of recent posts and render that within a view that has its model set for a list of posts, all without affecting the main view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top