LA.NET [EN]

Sep 16

In the last post we’ve seen how ActionResult derived classes will encapsulate the return values of action methods. Today, we’ll talk about two special types of action results: the PartialViewResult and ViewResult action result classes.

The PartialViewResul introduces most of the logic used when redering a view (to me, view is something that generates info sent back to the client). It introduces several important properties, presented on the following list:

  • TempData: property of type TempDataDictionary, used for storing temp data info. We’ve already talked about that property in the past. Generally, the controller will initialize this property when it creates this kind of action result;
  • View: property of type IView, that ends up holding a reference to the view responsible for returning the result to the client;
  • ViewData: property of type ViewDataDictionary, used for passing info from the controller to the view;
  • ViewEngine: property of type IViewEngine that is responsible for getting a view used for rendering the HTML sent back to the client;
  • ViewName: string which is used to identify the name of the view that should render the current response;

The PartialViewResult implements the ExecuteResult by doing several important things. First, it will initialize ther properties presented on the previous list. The most interesting piece of work is trying to find the view (which is done by the FindPartialView method). To do that, it ends up using the IViewEngine instance and calling the FindPartialView method. By the way, the IViewEngine interface looks like this:

public interface IViewEngine {
  ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName);
  ViewEngineResult FindView(ControllerContext controllerContext,string viewName,string masterName);
}

I’ll leave further investigation on how the view engine works for a future post. For now, you just need to keep in mind that you can use if to get a valid reference to a view that will render the response returned to the client.

The ViewEngineResult is a simple class that has only two properties: SearchedLocations and View. The View will hold a reference to an IView implementation that will render the view; SearchedLocations will end up giving you the locations where the view has been searched. This info will be given to you in the form of an exception whenever the view engine doesn’t find a view with the specified name in the search locations.

After getting a valid reference to an IView interface, the PartialViewResult class ends up calling the Render method exposed by the IView interface. Btw, here’s the contract introduced by this interface:

public interface IView {
  void Render(ViewContext viewContext, TextWriter writer);
}

As you can see, the Render method doesn’t return a string. Instead, it will receive an instance of the TextWriter class that the view will use to ouput its HTML. You can find more info on why this was done by reading this post by Brad Wilson.

As I‘ve said, the ViewResult class expands the PartialViewResult class by adding a new property called MasterName. In practice, this means that you’ll have the option of applying a master page when you return an item of this type. Internally, this class follows similar steps for getting the view, so I won’t be repeating myself here.

In practice, you’ll use the PartialViewResult for outputing  a small part of a view. That’s why you don’t have the master page options when dealing with them. On the other hand, you’ll use the ViewResult for getting a “complete” view. As you might expect, the Controller class exposes several methods that will let you reduce the ammount of typing needed for instanting these types of action results.

I think that by now we can all agree that the MVC platform is really a very modular platform and you can easilly customize its features/internal workings by replacing just small pieces of code. On the next post, we’ll keep looking at it and we’ll talk about view engines implementations. Keep tuned!

2 comments so far

  1. Mike
    12:31 pm - 9-19-2008

    I just finished reading your series on controllers and routing, it”s awesome. Reading it together with Reflector opened it gave me a good understanding of the whole ASP.NET MVC stack.

    Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>