11

With the Razor view engine in MVC3,

Is it possible to render a legacy ascx?


I was expecting to be able to do something like:

@Html.RenderPartial("Footer.ascx")
Jim G.
  • 15,141
  • 22
  • 103
  • 166

2 Answers2

16

Yes. Try this instead:

@Html.Partial("Footer")

or

@{ Html.RenderPartial("Footer"); }
gram
  • 2,772
  • 20
  • 24
  • 12
    Also, change the control's code-behind so it inherits from System.Web.Mvc.ViewUserControl instead of System.Web.UI.UserControl – Webveloper May 17 '12 at 18:35
  • thanks for saving me :) @Webdeveloper: you mentioned important point, without that it was throwing exception. – Raj Dec 21 '12 at 08:00
  • 1
    I had to add the extension for this to work for me `@Html.Partial("Footer.ascx")` (with MVC4) – pauloya Mar 06 '14 at 12:53
0

Just wanted to add that I haven't seen a lot of people posting this solution:

Html.RenderAction("Footer", "Home");

This is better practise if you are using MVC, because you can specify any data you need in the controller instead of trying to manage it in a free-floating partial view. Very beneficial if you use a BaseController class to initialize all your calls.

public class HomeController : Controller {
    // ...

    [ChildActionOnly]
    public PartialViewResult Footer() {
         // do work
        return PartialView();
    }

    // ...
}
sonjz
  • 4,870
  • 3
  • 42
  • 60