Using MVC RenderAction within a Webform

As pointed out in the previous post (<here>) it is possible to call the MVC RenderPartial from within a webform.  This assumes that your webform has all the information it needs to populate the required model.  Where this information is not available, a good alternative is to use RenderAction from within the webform:

Add the following to the MvcUtility class described in the previous post:

public static void RenderAction(string controllerName, string actionName, object routeValues)
{
   RenderPartial("RenderAction", new RenderActionViewModel() { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
}

Create the viewModel to support the data we want to pass:

public class RenderActionViewModel
{
   public string ControllerName { get; set; }
   public string ActionName { get; set; }
   public object RouteValues { get; set; }
}

And, create a partial view to make the RenderAction request (I put mine in the Shared directory)

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<RenderActionViewModel>" %>
<%Html.RenderAction(Model.ActionName, Model.ControllerName, Model.RouteValues); %>

Then, all that is needed is to call the MvcUtility.RenderAction from within a webform, passing it the name of the controller, action and and additional parameters that your action method requires.  Examples:

<% PartialViewHelper.RenderAction("myController", "myAction", new { param = Value }); %>
...
<% PartialViewHelper.RenderAction("Blogs", "List", new { page = 1, order = "date", comments = "off" }); %>

This is a great way of adding MVC functionality to a Webforms environment.

Advertisements

10 thoughts on “Using MVC RenderAction within a Webform

  1. John

    I tried to implement this solution and it does render the partial view but it doesn’t render where the webform markup happens. It always seems to render at the top of the page and not where it exists in the webform markup. Does this have to do with the IView.Render method writing the partial to the output stream before the webform writes it content to the output stream?

    Like

    Reply
      1. tim

        I’ve not had that problem. I always make sure my partial is within a div that is positioned correctly. Without explicit positioning it may appear out of the document flow

        Like

  2. tim

    Hi John,

    I have never experienced the problem you are describing, and I have used this technique in a number of projects, so I don’t believe its related to the IView.Render.

    Is your html/css causing the partial view to be relocated? Is it encapsulated in a DIV ?

    Like

    Reply
  3. tassadaque

    I have an ASP.net MVC 2 Application. I am trying to integrate some webform pages with there own master page in it. I have tried to call a partial view on this webform master page but i am getting the following error

    The controller for path ‘/reports/withmaster.aspx’ was not found or does not implement IController

    Like

    Reply
    1. tim

      Hi,
      Can you post the code you are using to call the partial – the ‘.aspx’ inyour error message indicates that it is looking in the wrong place for your controller

      Like

      Reply
  4. Jason Axley

    Very clever. Gave me some ideas. But I found that creating a view context (similarly to how you’ve created one) and using that to instantiate an HtmlHelper() is a way you can expose RenderAction directly.

    e.g. Html = new HtmlHelper(CreateViewContext(), new ViewPage());

    At that point. you can call Html.WhateverExtensionYouWant(); directly.

    And CreateViewContext() does a lot of what you did, except to preserve the route data that may already be there, you can use RouteTable.Routes.GetRouteData(httpContextBase) to build it.

    Like

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s