/// <summary> /// Renders a view to string. /// </summary> /// <param name="ControllerToRenderWith">Controller to render the view with</param> /// <param name="ViewTypeToLoad">Which view type to find and then render to a string</param> /// <param name="ViewNameToRender">View to render</param> /// <param name="Model">Model to be passed into the view</param> /// <param name="ViewBagData">View Bag Data that is accessible in the view</param> /// <returns>Rendered partial view as string</returns> public static string RenderViewToString(this Controller ControllerToRenderWith, ViewTypeToLoad.ViewTypeToRender ViewTypeToLoad, string ViewNameToRender, object Model, ViewDataDictionary ViewBagData) { //use the main helper method return RenderViewToStringHelper(ControllerToRenderWith, ViewTypeToLoad, ViewNameToRender, Model, ViewBagData, null); }
/// <summary> /// Renders a view to string. /// </summary> /// <param name="ControllerToRenderWith">Controller to render the view with</param> /// <param name="ViewTypeToLoad">Which view type to find and then render to a string</param> /// <param name="ViewNameToRender">Partial view to render</param> /// <param name="Model">Model to be passed into the view</param> /// <param name="LayoutPage">Layout page render the view with</param> /// <returns>Rendered partial view as string</returns> public static string RenderViewToString(this Controller ControllerToRenderWith, ViewTypeToLoad.ViewTypeToRender ViewTypeToLoad, string ViewNameToRender, object Model, string LayoutPage) { //use the main helper method return RenderViewToStringHelper(ControllerToRenderWith, ViewTypeToLoad, ViewNameToRender, Model, null, LayoutPage); }
/// <summary> /// Main Helper Method Which All The Overloads Funnel Down /// </summary> /// <param name="ControllerToRenderWith">Controller to render the view with</param> /// <param name="ViewType">Which view type to find and then render to a string</param> /// <param name="ViewNameToRender">View to render</param> /// <param name="Model">Model to be passed into the view</param> /// <param name="ViewBagData">View Bag Data that is accessible in the view</param> /// <param name="LayoutPage">Layout page render the view with</param> /// <returns>Rendered partial view as string</returns> private static string RenderViewToStringHelper(this Controller ControllerToRenderWith, ViewTypeToLoad.ViewTypeToRender ViewType, string ViewNameToRender, object Model, ViewDataDictionary ViewBagData, string LayoutPage) { //holds the view to render ViewEngineResult ViewResult; //is this a view or partial view if (ViewType == ViewTypeToLoad.ViewTypeToRender.PartialView) { //it's a view go find me the partial view ViewResult = ViewEngines.Engines.FindPartialView(ControllerToRenderWith.ControllerContext, ViewNameToRender); } else { //its a partial view, go grab the view ViewResult = ViewEngines.Engines.FindView(ControllerToRenderWith.ControllerContext, ViewNameToRender, LayoutPage); } //let's go validate we found the partial view (wan't to do it here rather then going into render razor..otherwise developer has to back track and look up the callstack to find out why it's null) if (ViewResult == null) { throw new ArgumentNullException("ViewResult", "Not Able to Find View: " + ViewNameToRender); } if (ViewResult.View == null) { throw new ArgumentNullException("ViewResult.View", "Not Able to Find View: " + ViewNameToRender); } if (ViewResult.ViewEngine == null) { throw new ArgumentNullException("ViewResult.ViewEngine", "Not Able to Find View: " + ViewNameToRender); } //go render the string and return it return RazorToString.RenderRazorViewToString(ControllerToRenderWith, ViewResult, Model, ViewBagData); }
/// <summary> /// Renders a view to string. /// </summary> /// <param name="ControllerToRenderWith">Controller to render the view with</param> /// <param name="ViewTypeToLoad">Which view type to find and then render to a string</param> /// <param name="ViewNameToRender">View to render</param> /// <returns>Rendered partial view as string</returns> public static string RenderViewToString(this Controller ControllerToRenderWith, ViewTypeToLoad.ViewTypeToRender ViewTypeToLoad, string ViewNameToRender) { //use the main helper method return RenderViewToStringHelper(ControllerToRenderWith, ViewTypeToLoad, ViewNameToRender, null, null, null); }
public string ViewOrPartialToString(ViewTypeToLoad.ViewTypeToRender ViewTypeToLoad) { return this.RenderViewToString(ViewTypeToLoad, "_TestView", "ViewToString123", string.Empty); }