views:

913

answers:

2

Some site I'm programming is using both ASP.NET MVC and WebForms.

I have a partial view and I want to include this inside a webform. The partial view has some code that has to be processed in the server, so using Response.WriteFile don't work. It should work with javascript disabled.

How can I do this?

+3  A: 

most obvious way would be via AJAX

something like this (using jQuery)

<div id="mvcpartial"></div>

<script type="text/javascript">
$(document).load(function () {
    $.ajax(
    {    
        type: "GET",
        url : "urltoyourmvcaction",
        success : function (msg) { $("#mvcpartial).html(msg); }
    });
});
</script>
Alexander Taran
From the question: It should work with javascript disabled.
Craig Stuntz
was added after my response )-:
Alexander Taran
+9  A: 

I had a look at the MVC source to see if I could figure out how to do this. There seems to be very close coupling between controller context, views, view data, routing data and the html render methods.

Basically in order to make this happen you need to create all of these extra elements. Some of them are relatively simple (such as the view data) but some are a bit more complex - for instance the routing data will consider the current WebForms page to be ignored.

The big problem appears to be the HttpContext - MVC pages rely on a HttpContextBase (rather than HttpContext like WebForms do) and while both implement IServiceProvider they're not related. The designers of MVC made a deliberate decision not to change the legacy WebForms to use the new context base, however they did provide a wrapper.

This works and lets you add a partial view to a WebForm:

public class WebFormController : Controller { }

public static class WebFormMVCUtil
{

    public static void RenderPartial( string partialName, object model )
    {
        //get a wrapper for the legacy WebForm context
        var httpCtx = new HttpContextWrapper( System.Web.HttpContext.Current );

        //create a mock route that points to the empty controller
        var rt = new RouteData();
        rt.Values.Add( "controller", "WebFormController" );

        //create a controller context for the route and http context
        var ctx = new ControllerContext( 
            new RequestContext( httpCtx, rt ), new WebFormController() );

        //find the partial view using the viewengine
        var view = ViewEngines.Engines.FindPartialView( ctx, partialName ).View;

        //create a view context and assign the model
        var vctx = new ViewContext( ctx, view, 
            new ViewDataDictionary { Model = model }, 
            new TempDataDictionary() );

        //render the partial view
        view.Render( vctx, System.Web.HttpContext.Current.Response.Output );
    }

}

Then in your WebForm you can do this:

<% WebFormMVCUtil.RenderPartial( "ViewName", this.GetModel() ); %>
Keith
+1 for the effort
eKek0
This works one a basic page request, but view.Render() blows up with the "Validation of viewstate MAC failed..." exception if you do any post backs on the container page. Can you confirm the same, Keith?
Kurt Schindler
I don't get that viewstate error - however I think it would occur is the partial view that you're rendering includes any WebForm controls. This RenderPartial method fires on render - after any viewstate. WebForm controls inside the partial view are going to be broken and outside of the normal page lifecycle.
Keith
Actually I have now - it seems to occur for some WebForms control hierarchies and not for others. Weirdly the error is thrown from inside the MVC render methods, as if the underlying call to Page. Render is expecting to do page and event MAC validation, which would always be entirely wrong in MVC.
Keith