在开发中有时要在后台获得某个View 或者 PartialView 生成的字符串,只要你熟悉Asp.Net MVC  生命周期就能理解和敲出下面的代码。没什么高深的,直接上代码:

 

1,输出View HTML 字符串:

 

复制代码
        /// <summary>
        /// 描述:输出View HTML 字符串
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="viewName">视图文件名</param>
        /// <param name="masterName">母板页文件名</param>
        /// <returns></returns>
        protected static string RenderViewToString(Controller controller, string viewName, string masterName)
        {
            IView view = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, masterName).View;
            using (StringWriter writer = new StringWriter())
            {
                ViewContext viewContext = new ViewContext(controller.ControllerContext, view, controller.ViewData, controller.TempData, writer);
                viewContext.View.Render(viewContext, writer);
                return writer.ToString();
            }
        }
复制代码

 

 

2,输出PartialView HTML 字符串:

 

复制代码
        /// <summary>
        /// 描述:输出PartialView HTML 字符串
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="partialViewName">部分视图文件名</param>
        /// <returns></returns>
        protected static string RenderPartialViewToString(Controller controller, string partialViewName)
        {
            IView view = ViewEngines.Engines.FindPartialView(controller.ControllerContext, partialViewName).View;
            using (StringWriter writer = new StringWriter())
            {
                ViewContext viewContext = new ViewContext(controller.ControllerContext, view, controller.ViewData, controller.TempData, writer);
                viewContext.View.Render(viewContext, writer);
                return writer.ToString();
            }
        }
复制代码
 
通常,我们定义个基类
public class ApplicationController : Controller
   {
       protected string RenderPartialViewToString(string viewName, object model)
       {
           if (string.IsNullOrEmpty(viewName))
               viewName = ControllerContext.RouteData.GetRequiredString("action");
 
           ViewData.Model = model;
 
           using (var sw = new StringWriter())
           {
               ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
               var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
               viewResult.View.Render(viewContext, sw);
 
               return sw.GetStringBuilder().ToString();
           }
       }
 
   }
然后,每个controller派生
public class AccountController : ApplicationController

{
     [HttpPost]
       [AllowAnonymous]
       public JsonResult Details(string id)
       {
           UserModel user = _usrs.GetUser(id);
           var m = new { Status = 1, Message = "Ok", Content = RenderPartialViewToString("Details", user) };
           return Json(m);
       }

}

View页面中
<div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="button" value="Show View" id="ShowView" class="btn btn-default" />
                    </div>
                </div>
<div id="detailsinfo" style="width: 280px; z-index: 1000;">
             </div>
@section Scripts
{
    @Scripts.Render("~/bundles/jqueryval")
    <script type="text/javascript">
    $("#ShowView").bind("click",function(event)
    {
        $.ajax({
 
            type: "POST",
            url: '@Url.Action("Details", "Account")',
            data: { id: 'AnnB'},
            datatype: "json",
            success: function (data) {
                $("#detailsinfo").html(data.Content);
            }
 
        });
    });
 
        </script>
}

通过ajax实现partialview:Details页面的生成