Static Method to render string

public static string RenderPartialToString(string partialFile, object viewData)
{
     ViewDataDictionary vd = new ViewDataDictionary(viewData);
     ViewPage vp = new ViewPage { ViewData = vd };
     Control control = vp.LoadControl(partialFile);

     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();
}

 

// Controller Method
public string GetHtmlFromPartial()
{
     
     var viewData = new SomeViewData { Note = n };

     string s = RenderPartialToString("~/Views/Error.ascx", viewData);

     return s;
}

相关文章:

  • 2022-12-23
  • 2021-12-11
  • 2021-10-14
  • 2021-09-07
  • 2022-12-23
猜你喜欢
  • 2021-05-27
  • 2022-12-23
  • 2021-06-07
  • 2021-06-26
  • 2021-06-03
相关资源
相似解决方案