簡體   English   中英

在剃刀視圖中從ActionResult獲取HTML

[英]Get html from ActionResult in razor view

我有以下課程:

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

並從我的剃刀視圖中調用它:

@(Html.RenderWidget<TestWidgetController>())

但輸出是:

System.Web.Mvc.ViewResult

我如何從ActionResult中獲取HTML?

除非您做的非常具體,否則可以使用它在Razor視圖中呈現html:

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

而不是傳遞Controller實例,而將Controller Action的格式設置為:

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

否則,事情會變得更加復雜。 您可以在此視圖中使用@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