4

We have old ASP.NET Forms pages and new MVC views and partial views in the same solution. Some pages on the site are MVC and legacy pages are Forms. One of these legacy Forms pages is an .ascx control.

Is there any way for me to insert an MVC partial view (.ascx) into this Forms .ascx control?

3 Answers 3

2

I use this technique to embed MVC partials into webforms pages. Not sure if it works in a webforms user control, but it should be possible.

Step 1. Within the MVC part of your application, create the following helper function. This does all the hard work:

namespace MvcApplication
{
    // create a dummy controller
    public class DummyController : Controller
    {
    }

    public static class MvcPartialHelper
    {
        public static void RenderPartial(string partialViewName, object model)
        {
            ControllerContext controllerContext;
            HttpContextBase httpContextBase;
            IView view;
            RouteData routeData;
            ViewContext viewContext;

            httpContextBase = new HttpContextWrapper(HttpContext.Current);
            routeData = new RouteData();
            routeData.Values.Add("controller", typeof(DummyController).Name);
            controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController());
            view = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName).View;
            viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
            view.Render(viewContext, httpContextBase.Response.Output);
        }
    }
}

then, within your web page (or user control):

add the following to reference the above:

<%@ Import Namespace="MvcApplication" %>

and then when you need to display the partial you can add something like:

<% MvcPartialHelper.RenderPartial("~/views/shared/TestPartial.ascx", "hello - this is my model"); %>

where the second parameter is your 'Model'.

I use this technique extensively in a mixed MVC/Webforms environment and it works like a dream!

Enjoy

Sign up to request clarification or add additional context in comments.

Comments

0

No, there isn't as you don't have the Html helper needed to perform this insertion:

<%= Html.RenderPartial("foo") %>

Also your MVC partial is strongly typed (isn't it) and you won't have access to the model.

Also when you are migrating a legacy webforms application to ASP.NET MVC it should be the other way round.

2 Comments

Can't I just include the namespace of the HtmlHelper?
Html is a property of System.Web.ViewPage, so the namespace inclusion trick is only when you wrote custom helpers that extent HtmlHelper, but this suppose that you already have a reference to it which is not the case in a class web form.
0

Technically it is possible, though you need to jump through some hoops to achieve what you are asking. You need to create a dummy controller context, view Context and related environment, and then create a property in your page behind code to simulate the model.

Let me know if you would like detailed instructions / example

1 Comment

Yes, do you have a link you can point me to? Pretty please :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.