在ASP.NET控件中使用ASP.NET MVC视图 [英] Using asp.net mvc view in asp.net control

查看:86
本文介绍了在ASP.NET控件中使用ASP.NET MVC视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码将一个asp.net Webforms控件添加到Webforms应用程序中的内容占位符:

Following code adds an asp.net webforms control to my content placeholder in webforms application:

var someControl = (System.Web.UI.UserControl)LoadControl("~/serverPath/" + ControlName + ".ascx");
phContentControls.Controls.Add(someControl);

所以我想知道有什么方法可以将呈现的asp.net mvc视图添加到我的控件中吗?

So i'm wondering is there any way i can add rendered asp.net mvc view to my control?

omg,wtf我刚刚问过:D

omg, wtf i've just asked :D

推荐答案

您可以尝试执行以下操作:

You could try doing something like the following:

这是我想在Webforms页面中呈现PartialViews或ChildActions时使用的MvcUtility类,但是我认为我没有在UserControl中使用它.

This is an MvcUtility Class I use when I would like to render PartialViews or ChildActions in a Webforms Page, I don't think I have used it in a UserControl however.

不知道您使用的是哪个MVC版本,但我知道它可以与MVC 3和Razor Views一起使用.

Not sure what MVC version you are using but I know this works with MVC 3 and Razor Views.

public static class MvcUtility
{
        public static void RenderPartial(string partialViewName, object model)
        {
            // Get the HttpContext
            HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current);
            // Build the route data, pointing to the Some controller
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", typeof(Controller).Name);
            // Create the controller context
            ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new Controller());
            // Find the partial view
            IView view = FindPartialView(controllerContext, partialViewName);
            // create the view context and pass in the model
            ViewContext viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
            // finally, render the view
            view.Render(viewContext, httpContextBase.Response.Output);
        }

        private static IView FindPartialView(ControllerContext controllerContext, string partialViewName)
        {
            // try to find the partial view
            ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
            if (result.View != null)
            {
                return result.View;
            }
            // wasn't found - construct error message
            StringBuilder locationsText = new StringBuilder();
            foreach (string location in result.SearchedLocations)
            {
                locationsText.AppendLine();
                locationsText.Append(location);
            }
            throw new InvalidOperationException(String.Format("Partial view {0} not found. Locations Searched: {1}", partialViewName, locationsText));
        }

        public static void RenderAction(string controllerName, string actionName, object routeValues)
        {
            RenderPartial("RenderActionUtil", new RenderActionVM() { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
        }
    }

要呈现ChildAction,您将需要在Shared MVC Views文件夹中获得部分视图:

To render a ChildAction you will need a partial view in the Shared MVC Views Folder:

@model YourNamespace.RenderActionVM

@{
    Html.RenderAction(Model.ActionName, Model.ControllerName, Model.RouteValues);
}

和视图模型:

public class RenderActionVM
{
    public string ControllerName { get; set; }
    public string ActionName { get; set; }
    public object RouteValues { get; set; }
}

最后在您的webforms页面中调用:

And finally in your webforms page call like this:

<% MvcUtility.RenderPartial("_SomePartial", null); %> 

<% MvcUtility.RenderAction("SomeController", "SomeAction", new { accountID = Request.QueryString["id"], dateTime = DateTime.Now }); %>

这篇关于在ASP.NET控件中使用ASP.NET MVC视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆