MVC3 Razor: Is it Possible to Render a Legacy ASCX?
Asked Answered
I

2

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")
Inextricable answered 30/6, 2011 at 17:43 Comment(0)
B
16

Yes. Try this instead:

@Html.Partial("Footer")

or

@{ Html.RenderPartial("Footer"); }
Buonomo answered 30/6, 2011 at 18:4 Comment(3)
Also, change the control's code-behind so it inherits from System.Web.Mvc.ViewUserControl instead of System.Web.UI.UserControlDaladier
thanks for saving me :) @Webdeveloper: you mentioned important point, without that it was throwing exception.Uzzi
I had to add the extension for this to work for me @Html.Partial("Footer.ascx") (with MVC4)Underthecounter
E
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();
    }

    // ...
}
Eanore answered 14/8, 2012 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.