Ejemplo n.º 1
0
        /// <summary>
        ///     Renders a partial MVC view to string. Use this method to render
        ///     a partial view that doesn't merge with _Layout and doesn't fire
        ///     _ViewStart.
        /// </summary>
        /// <param name="viewPath">
        ///     The path to the view to render. Either in same controller, shared by
        ///     name or as fully qualified ~/ path including extension
        /// </param>
        /// <param name="model">The model to pass to the viewRenderer</param>
        /// <param name="controllerContext">Active Controller context</param>
        /// <returns>String of the rendered view or null on error</returns>
        public static string RenderView(string viewPath, object model       = null,
                                        ControllerContext controllerContext = null)
        {
            var renderer = new ViewRenderer(controllerContext);

            return(renderer.RenderViewToString(viewPath, model));
        }
Ejemplo n.º 2
0
 /// <summary>
 ///     Renders a partial MVC view to string. Use this method to render
 ///     a partial view that doesn't merge with _Layout and doesn't fire
 ///     _ViewStart.
 /// </summary>
 /// <param name="viewPath">
 ///     The path to the view to render. Either in same controller, shared by
 ///     name or as fully qualified ~/ path including extension
 /// </param>
 /// <param name="model">The model to pass to the viewRenderer</param>
 /// <param name="controllerContext">Active Controller context</param>
 /// <param name="errorMessage">optional out parameter that captures an error message instead of throwing</param>
 /// <returns>String of the rendered view or null on error</returns>
 public static string RenderView(string viewPath, object model,
                                 ControllerContext controllerContext,
                                 out string errorMessage)
 {
     errorMessage = null;
     try
     {
         var renderer = new ViewRenderer(controllerContext);
         return(renderer.RenderViewToString(viewPath, model));
     }
     catch (Exception ex)
     {
         errorMessage = ex.GetBaseException().Message;
     }
     return(null);
 }