/// <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 RenderPartialView(string viewPath, object model = null, ControllerContext controllerContext = null) { ViewRenderer renderer = new ViewRenderer(controllerContext); return(renderer.RenderPartialViewToString(viewPath, model)); }
protected override void DoRender(HtmlTextWriter output) { using (new RenderingDiagnostics(output, Path + " (statically bound)", Cacheable, VaryByData, VaryByDevice, VaryByLogin, VaryByParm, VaryByQueryString, VaryByUser, ClearOnIndexUpdate, GetCachingID())) { Assert.IsNotNull(Path, "Path was null or empty, and must point to a valid virtual path to a Razor rendering."); var renderer = new ViewRenderer(); var result = renderer.RenderPartialViewToString(Path, Model); output.Write(result); } }
protected override void RenderModel(System.Web.UI.HtmlTextWriter writer) { Assert.IsNotNull(ViewPath, "ViewPath was null or empty, and must point to a valid virtual path to a Razor rendering."); var viewData = new Dictionary <string, object>(); viewData[MetadataConstants.RenderingParametersViewDataKey] = RenderingParameters; var renderer = new ViewRenderer(ControllerContext); var result = renderer.RenderPartialViewToString(ViewPath, Model, viewData); writer.Write(result); }
public async Task RenderPartialViewToString_should_successfully_render_view_into_string() { // Arrange. string data = "this is the data"; string partialViewPath = "temp/view/myview"; // Http context Mock <HttpContext> mockContext = new Mock <HttpContext>(); Mock <IFeatureCollection> mockFeaturesColl = new Mock <IFeatureCollection>(); Mock <IRoutingFeature> mockRoutingFeature = new Mock <IRoutingFeature>(); Mock <RouteData> mockRoutData = new Mock <RouteData>(); mockRoutingFeature.Setup(m => m.RouteData).Returns(mockRoutData.Object); mockFeaturesColl.Setup(m => m.Get <IRoutingFeature>()).Returns(mockRoutingFeature.Object); mockContext.Setup(m => m.Features).Returns(mockFeaturesColl.Object); mockHttpContextAccessor.Setup(m => m.HttpContext).Returns(mockContext.Object); // Razor view engine mocks Mock <IViewEngine> mockEngine = new Mock <IViewEngine>(); Mock <IView> mockView = new Mock <IView>(); mockRazorViewEngine.Setup(m => m.FindView(It.IsAny <ActionContext>(), It.IsAny <string>(), It.IsAny <bool>())).Returns(ViewEngineResult.Found(partialViewPath, mockView.Object)); IViewRenderer renderer = new ViewRenderer(mockViewOptions.Object, mockRazorViewEngine.Object, mockTempDataProvider.Object, mockHttpContextAccessor.Object); // IOptions Mock <MvcViewOptions> mockMvcOptions = new Mock <MvcViewOptions>(); MvcViewOptions opt = new MvcViewOptions(); opt.HtmlHelperOptions = new HtmlHelperOptions(); //mockMvcOptions.Setup(m => m.HtmlHelperOptions).Returns(new HtmlHelperOptions()); mockViewOptions.Setup(m => m.Value).Returns(opt); // Act. string htmlString = await renderer.RenderPartialViewToString(data, partialViewPath); // Assert. // since we are not passing in any view ... the result html string should be empty. Assert.IsEmpty(htmlString); }
public IHttpActionResult GetProductList(string brand, string category, decimal price = 0, bool isSortDes = false, int pageNumber = 1) { try { var productlist = _productListingService.GetListProduct(brand, price, category, isSortDes, pageNumber); var viewrenderer = new ViewRenderer(); var html = viewrenderer.RenderPartialViewToString("~/Views/Shared/_ProductList.cshtml", productlist.Products); var response = new ProductListResponse() { Html = html, HasMore = (pageNumber * productlist.PageSize) < productlist.TotalProducts }; return(Ok(response)); } catch (Exception) { return(BadRequest()); } }