このメソッドを使用してビューをHTMLにレンダリングします(これはpdfsの生成に必要です)。私はビューでモデルを変更したときにhtmlへのRazorページのレンダリングには別のモデルが必要です
public static class ControllerContextExtensions
{
public static string RenderViewToHtml(this ControllerContext context, string viewName, object model)
{
context.Controller.ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(context,
viewName);
ViewContext viewContext = new ViewContext(context, viewResult.View, context.Controller.ViewData, context.Controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
string html = sw.GetStringBuilder().ToString();
string baseUrl = string.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority);
html = Regex.Replace(html, "<head>", string.Format("<head><base href=\"{0}\" />", baseUrl), RegexOptions.IgnoreCase);
return html;
}
}
しかし、それはいつものフレーズとRender(viewContext, sw);
で(失敗した部分図は、タイプBのモデルを宣言しているにもかかわらず、「それは、モデルAが必要ですが、渡されたモデルは、タイプBです」 注:「ABC」は部分図は実験としてそれで@model InvoiceReportModelItem
を持って、私はBaseReportViewModelの子孫タイプの空のモデルに合格したが今ではそれは私が以前に合格しようとしたタイプのモデルが必要と言う
。問題の原因は何か?私はファイルの名前を変更してください(辞書がキャッシュされているかどうかなど)。
私はあなたが何を求めているのかは分かりませんが、複数のモデルが入っているモデルを使用することができます。 – Danieboy
@Danieboy、問題は、ビューがBaseReportViewModel(間違った)またはInvoiceReportModelItem( "ABC"ビューのモデルとして宣言された)の子孫ではないということです。 – pwrigshihanomoronimo
** abcで指定されたモデルは何ですか? **見る? –