これが答えです!マーティン・フロムの方法からわずかに変更されており、機能しているようです。足りないものがある場合は、コメント セクションでコードの変更を投稿してください。ありがとう。
コントローラーから次のように呼び出します。
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();
}