简体   繁体   English

在剃刀视图中从ActionResult获取HTML

[英]Get html from ActionResult in razor view

I have the following class: 我有以下课程:

public static class WidgetHelper
{
    public static ActionResult RenderWidget<T>(this HtmlHelper htmlHelper)
    {
        var widgetControllerInstance = Activator.CreateInstance<T>() as WidgetController;
        return widgetControllerInstance.Index();
    }
}

And call it from my razor view: 并从我的剃刀视图中调用它:

@(Html.RenderWidget<TestWidgetController>())

but the output is: 但输出是:

System.Web.Mvc.ViewResult

How do i get the html out of the ActionResult? 我如何从ActionResult中获取HTML?

Unless you are doing something very specific, you can use this to render the html in the Razor view: 除非您做的非常具体,否则可以使用它在Razor视图中呈现html:

@{ Html.RenderAction("Index", "TestWidget", new { }); }

rather than pass around a Controller instance, with your Controller Action formatted like this: 而不是传递Controller实例,而将Controller Action的格式设置为:

[ChildActionOnly]
public ActionResult Index()
{
    var viewModel = YourViewModel();
    return PartialView(viewModel);
}

Otherwise, things get more involved. 否则,事情会变得更加复杂。 You can use @Html.RenderWidget() in the View with this, rather than @(Html.RenderWidget<TestWidgetController>()) : 您可以在此视图中使用@Html.RenderWidget() ,而不是@(Html.RenderWidget<TestWidgetController>())

public static class WidgetHelper
{
    public static T CreateController<T>(RouteData routeData = null) where T : Controller, new()
    {
        //Ccreate a disconnected controller instance
        T controller = new T();
        // Get context wrapper from HttpContext if available
        HttpContextBase wrapper;
        if (System.Web.HttpContext.Current != null)
        {
            wrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
        }
        else
        {
            throw new InvalidOperationException("Cannot create Controller Context if no active HttpContext instance is available.");
        }

        if (routeData == null)
        {
            routeData = new RouteData();
        }

        // Add the controller routing if not existing
        if (!routeData.Values.ContainsKey("controller") && !routeData.Values.ContainsKey("Controller"))
        {
            routeData.Values.Add("controller", controller.GetType().Name.ToLower().Replace("controller", ""));
        }

        controller.ControllerContext = new ControllerContext(wrapper, routeData, controller);
        return controller;
    }

    public static string ViewToString(ControllerContext context, string viewPath, object model = null, bool partial = false)
    {
        // First find the ViewEngine for this view
        ViewEngineResult viewEngineResult = null;
        if (partial)
        {
            viewEngineResult = ViewEngines.Engines.FindPartialView(context, viewPath);
        }
        else
        {
            viewEngineResult = ViewEngines.Engines.FindView(context, viewPath, null);
        }

        if (viewEngineResult == null)
        {
            throw new FileNotFoundException("View cannot be found.");
        }

        // Get the view and attach the model to view data
        var view = viewEngineResult.View;
        context.Controller.ViewData.Model = model;

        string result = null;

        using (var sw = new StringWriter())
        {
            var ctx = new ViewContext(context, view, context.Controller.ViewData, context.Controller.TempData, sw);
            view.Render(ctx, sw);
            result = sw.ToString();
        }

        return result.Trim();
    }

    public static HtmlString RenderWidget(this HtmlHelper htmlHelper)
    {
        var html = ViewToString(CreateController<Core.Controllers.WidgetController>().ControllerContext, "~/Views/Widget/Index.cshtml", null, true);
        return new HtmlString(html);
    }
}

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

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