后台获取视图对应的字符串
1.帮助类
/// <summary> /// 后台获取视图对应的字符串 /// </summary> public class ViewHelper { /// <summary> /// 将View输出为字符串 /// (注:不会执行对应的action方法) /// </summary> /// <param name="controller">Controller实例</param> /// <param name="viewName">如果view文件在当前Controller目录下,则直接输入文件名(例如:Toolbar) /// 否则,从根路径开始指定(例如:~/Views/User/Toolbar.cshtml) /// </param> /// <param name="masterName">模板页文件名(注:显示指定可修改Layout)</param> /// <returns></returns> public static string RenderViewToString(Controller controller, string viewName, string masterName) { IView view = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, masterName).View; using (StringWriter sw = new StringWriter()) { ViewContext viewContext = new ViewContext(controller.ControllerContext, view, controller.ViewData, controller.TempData, sw); viewContext.View.Render(viewContext, sw); return sw.ToString(); } } /// <summary> /// 将PartialView输出字符串 /// </summary> /// <param name="controller">controller实例</param> /// <param name="viewName">如果PartialView文件在当前Controller目录下,则直接输入文件名(例如:Toolbar); /// 否则,从根路径开始指定(例如:~/View/User/Toolbar.cshtml) /// </param> /// <param name="model">构造页面所需要的实体参数</param> /// <returns>字符串</returns> public static string ReaderPartialViewToString(Controller controller, string viewName, object model) { IView view = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName).View; controller.ViewData.Model = model; using (StringWriter sw = new StringWriter()) { ViewContext viewContext = new ViewContext(controller.ControllerContext, view, controller.ViewData, controller.TempData, sw); viewContext.View.Render(viewContext, sw); return sw.ToString(); } } }
2.使用验证
1.
public ActionResult Index() { //后台加载其他视图为字符串没有母版页 ViewBag.tempOne = ViewHelper.RenderViewToString(this, "TempOne", null); return View(); } public ActionResult IndexTwo() { //后台加载其他视图的字符串,带有母版页 //ViewBag.tempTwo = ViewHelper.RenderViewToString(this, "TempTwo", null); //后台指定修改母版页 ,并生成字符串 ViewBag.tempTwo = ViewHelper.RenderViewToString(this, "TempTwo", "_Layout"); return View(); } //不使用母版页 public ActionResult TempOne() { return View(); } //使用母版页 public ActionResult TempTwo() { ////获取 可用字体字符数组 //string[] nameList = FontFamily.Families.Select(q => q.Name).ToArray(); //ViewBag.nameList = nameList; return View(); }
2.
public ActionResult Index() { //后台加载其他controller 的 视图,并生成字符串 ViewBag.tempTwo = ViewHelper.RenderViewToString(this, "~/Views/ViewOne/TempTwo.cshtml", null); return View(); }
3.
public ActionResult Index() { //显示主视图 //ViewBag.tempOne = ViewHelper.RenderViewToString(this, "~/views/viewone/TempOne.cshtml", null); //作为主视图显示,会加载母版页 //ViewBag.tempTwo = ViewHelper.RenderViewToString(this, "PartialTwo",null); //作为部分视图显示,不会加载母版页,会过滤到部分标签(head,body) //ViewBag.tempTwo = ViewHelper.ReaderPartialViewToString(this, "PartialTwo", null); //ViewBag.tempTwo = ViewHelper.ReaderPartialViewToString(this, "~/views/viewone/TempOne.cshtml", null); //作为部分视图显示,传递参数 ViewBag.tempTwo = ViewHelper.ReaderPartialViewToString(this, "PartialTwo", new int[] { 1, 2, 3 }); return View(); } //部分视图 public PartialViewResult PartialTwo() { return PartialView(); }