ASP.NET MVC RenderPartial to String
2010-09-03 10:38 敏捷的水 阅读(762) 评论(0) 编辑 收藏 举报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; }
扫码关注公众号,了解更多管理,见识,育儿等内容
作者: 王德水
出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。
出处:http://www.cnblogs.com/cnblogsfans
版权:本文版权归作者所有,转载需经作者同意。