我有以下课程:
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?
答案 0 :(得分:1)
除非您正在做一些非常具体的事情,否则您可以使用它在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);
}
}