这是答案!与Martin From 的方法略有不同,它似乎有效。如果缺少某些东西,请人们在评论部分提供任何代码更改。谢谢。
从你的控制器调用它是这样的:
string HTMLOutput = Utils.RenderPartialToString("~/Views/Setting/IndexMain.ascx", "", items, this.ControllerContext.RequestContext);
将此添加到课程中
public static string RenderPartialToString(string controlName, object viewData, object model, System.Web.Routing.RequestContext viewContext)
{
ViewDataDictionary vd = new ViewDataDictionary(viewData);
ViewPage vp = new ViewPage { ViewData = vd };
vp.ViewData = vd;
vp.ViewData.Model = model;
vp.ViewContext = new ViewContext();
vp.Url = new UrlHelper(viewContext);
Control control = vp.LoadControl(controlName);
vp.Controls.Add(control);
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
using (HtmlTextWriter tw = new HtmlTextWriter(sw))
{
vp.RenderControl(tw);
}
return sb.ToString();
}