2

I am using Visual Studio 2013. I have an existing WebForms Web Project which I added MVC too following this article by Dave Paquette - http://www.davepaquette.com/archive/2013/12/30/so-you-inherited-an-asp-net-web-forms-application.aspx

However what is the best way to Render a Partial View from the MVC Area I have added in an existing aspx page?

I was thinking I could have a div on my aspx page:

<div id="mvcPartial"></div>

and then have an ajax call something like below:

  $.ajax({
            url: '/MVCArea/MyController/MyAction',
            type: 'GET',
            success: function (data) {
                $('#mvcPartial').html(data);
            },
            error: function (xhr) {
                alert("Failedto return view");
            }
        });

Is there a better way off getting a MVC View rendered in a aspx page?

TheRiddler
  • 169
  • 3
  • 16

2 Answers2

0

javascript helper:

function getPartial(url,data,controlId)
{
    $.ajax({
        type: "GET",
        data: data,
        url: url,
        cache: false
        }).done(function (result) {
            $(controlId).html(result);
        }).fail(function (jqXHR, textStatus) {
            $(controlId).html("Request failed: " + textStatus);
        });
}
aleha_84
  • 8,309
  • 2
  • 38
  • 46
0

What you have done is probably the simplest and safest possible approach as it avoids attempting to mix WebForms and MVC contexts within the same web request.

The disadvantage of course is that you require 2 http requests to get the contents of the page.

If you wanted to render a simple MVC Partial View as part the aspx page itself, without requiring a separate request, you could try the following approach.

1) Create an HtmlHelper instance in your code behind. The following code is adapted from Access HtmlHelpers from WebForm when using ASP.NET MVC

  protected HtmlHelper htmlHelper;

    protected void Page_Load(object sender, EventArgs e)
    {
        var httpContext = new HttpContextWrapper(HttpContext.Current);
        RouteData routeData = new RouteData();
        routeData.Values["controller"] = "Dummy";
        var controllerContext = new ControllerContext(httpContext, routeData, new DummyController());
        var viewContext = new ViewContext(controllerContext, new WebFormView(controllerContext, "View"), new ViewDataDictionary(), new TempDataDictionary(), TextWriter.Null);
        htmlHelper  = new HtmlHelper(viewContext, new ViewDataBag());                                   
    }


    private class ViewDataBag : IViewDataContainer
    {
        ViewDataDictionary viewData = new ViewDataDictionary();
        public ViewDataDictionary ViewData
        {
            get
            {
                return viewData;
            }
            set
            {
                viewData = value;
            }
        }
    }
    private class DummyController : Controller
    {
    }

Note that this code is non-trivial and certainly feels very 'hacky'. You could put this in a base Page or base UserControl class if you are using it often.

  1. In the .aspx file, you can now render a partial view by calling htmlHelper.Partial:

    <div id="mvcPartial">
        <%:htmlHelper.Partial("_SimplePartial") %>
    </div>
    

I tested this with Web Forms 4.5 and MVC 5 and it works in simple cases. I can't be certain that it will work in all cases.

Community
  • 1
  • 1
David Paquette
  • 524
  • 6
  • 14
  • I have to note that this approach will generally work in only the most trivial cases. If the view needs model state, more interesting view data, needs to generate URLs, etc. it won't work (it might *run*, but it will not run *correctly*). – Eilon Dec 28 '14 at 01:14
  • Eilon, I agree. That's why I said it won't work in all cases and that the original approach was the safest approach. – David Paquette Dec 28 '14 at 15:42
  • Thanks dave appreciate the answer - ill probably stick with the ajax loading approach but this will be useful to refer too – TheRiddler Dec 29 '14 at 19:38