您可以使用以下方法(取自此处):
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter()) {
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
另一种可能性是呈现一个视图,您可以在其中调用自定义的 Html 辅助扩展方法,您可以在其中使用Html.Action
:
public static IHtmlString MyMethod(this HtmlHelper html)
{
// you could use html.Action("someAction", "someController") here
...
}