public static class RenderViewTostring
{
/// <summary>
///将部分视图转成html 字符串方便我们扩展使用
/// </summary>
/// <param name="controller">The controller.</param>
/// <param name="viewName">Name of the view.</param>
/// <param name="model">The model.</param>
/// <returns></returns>
public static string RenderPartialView(this Controller controller, string viewName)
{
return RenderPartialView(controller, viewName, null);
}
/// <summary>
///将部分视图转成html 字符串方便我们扩展使用
/// </summary>
/// <param name="controller">The controller.</param>
/// <param name="viewName">Name of the view.</param>
/// <param name="model">The model.</param>
/// <returns></returns>
public static string RenderPartialView(this Controller controller, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = controller.ControllerContext.RouteData.GetRequiredString("action"); controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
// return sw.Encoding.EncodingName;
return sw.GetStringBuilder().ToString();
}
}
/// <summary>
/// 部分视图转成html
/// viewbag 和viewdata 访问是相通的.
/// ViewData["Hello"] = "Hello Boy!";
/// view ViewBag.Hello 就是访问上面这个;
/// </summary>
/// <param name="controller">The controller.</param>
/// <param name="viewName">Name of the view.</param>
/// <param name="model">The model.</param>
/// <param name="viewdataary">键值形式的对象</param>
/// <returns></returns>
public static string RenderPartialView(this Controller controller, string viewName, object model, params KeyValuePair<string, object>[] viewdataary)
{
if (string.IsNullOrEmpty(viewName))
viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
controller.ViewData.Model = model;
if (viewdataary != null && viewdataary.Any())
{
foreach (var item in viewdataary)
{
controller.ViewData.Add(item);
}
}
return ControllserRenderView(controller, viewName);
} /// <summary>
/// Controllsers the render view.
/// </summary>
/// <param name="controller">The controller.</param>
/// <param name="viewName">Name of the view.</param>
/// <returns></returns>
private static string ControllserRenderView(Controller controller, string viewName)
{
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
}

最新文章

  1. [转载]50个Demo展示HTML5无穷的魅力
  2. mysql批量执行sql文件
  3. DELPHI设置枚举类型size
  4. (转)MyEclipse设置注释格式
  5. 排序(sort qsort)
  6. 从头学Android系列
  7. OSPF路由汇总和默认路由设置
  8. c&amp;c++函数的参数和返回值的传递终结版
  9. BI名词解释
  10. Android技术精髓-Bitmap详解
  11. SQL Serve数据库排序空值null始终前置的方法
  12. js淡入淡出
  13. selenium2入门 定位 窗体切换等等 (二)
  14. 前端开发chrome console的使用 :评估表达式 – Break易站
  15. Linux Shell 编程语法
  16. [转载]一个高效简洁的Aseprite to Unity导入工具
  17. puppet使用 apache passsenger 作为前端 (debian)
  18. C# SemaphoreSlim 实现
  19. HDFS初次编程
  20. ubuntu 下使用 jsoncpp库

热门文章

  1. RN集成echarts4图表组件react-native-secharts(转载)
  2. JIRA 7.8 版本的安装与破解
  3. 云原生GIS技术
  4. windows7 安装pytorch
  5. 安卓开发学习之Menu
  6. linux文件查找find命令
  7. 原来你是这样的setTimeout
  8. input date 赋值的坑及改变时如何获取 input date的值
  9. Spock - Document -04- Interaction Based Testing
  10. .net第四章内容总结