简体   繁体   English

从WebForms渲染MVC视图

[英]Render MVC View From WebForms

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 我正在使用Visual Studio2013。我有一个现有的WebForms Web项目,我在Dave Paquette的这篇文章之后也添加了MVC- http: //www.davepaquette.com/archive/2013/12/30/so-you-inherited- 一个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? 但是,从我在现有aspx页中添加的MVC区域渲染部分视图的最佳方法是什么?

I was thinking I could have a div on my aspx page: 我以为我可以在aspx页面上设置div:

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

and then have an ajax call something like below: 然后进行ajax调用,如下所示:

  $.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? 有没有更好的方法来使MVC视图呈现在apx页中?

javascript helper: javascript助手:

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);
        });
}

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. 您所做的可能是最简单,最安全的方法,因为它避免了尝试在同一Web请求中混合使用WebForms和MVC上下文。

The disadvantage of course is that you require 2 http requests to get the contents of the page. 当然,缺点是您需要2个http请求才能获取页面内容。

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. 如果您希望将简单的MVC局部视图作为aspx页面本身的一部分进行呈现,而无需单独的请求,则可以尝试以下方法。

1) Create an HtmlHelper instance in your code behind. 1)在后面的代码中创建一个HtmlHelper实例。 The following code is adapted from Access HtmlHelpers from WebForm when using ASP.NET MVC 使用ASP.NET MVC时 ,以下代码改编自WebForm的Access HtmlHelpers

  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'. 请注意,此代码并非无关紧要的,当然感觉非常“ hacky”。 You could put this in a base Page or base UserControl class if you are using it often. 如果经常使用,可以将其放在基本Page或基本UserControl类中。

  1. In the .aspx file, you can now render a partial view by calling htmlHelper.Partial: 在.aspx文件中,您现在可以通过调用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. 我使用Web Forms 4.5和MVC 5进行了测试,它在简单的情况下也可以工作。 I can't be certain that it will work in all cases. 我不能确定它在所有情况下都能正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM