I need to render a partial view as string. For this I use following Helper class:
public static string RenderRazorViewToString(Controller controller, string viewName, object model = null)
{
controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
IViewEngine viewEngine =
controller.HttpContext.RequestServices.GetService(typeof(ICompositeViewEngine)) as
ICompositeViewEngine;
ViewEngineResult viewResult = viewEngine.FindView(controller.ControllerContext, viewName, false);
ViewContext viewContext = new ViewContext(
controller.ControllerContext,
viewResult.View,
controller.ViewData,
controller.TempData,
sw,
new HtmlHelperOptions()
);
viewResult.View.RenderAsync(viewContext);
return sw.GetStringBuilder().ToString();
}
}
This works but I now need a similiar Method that also accepts a list of parameters. The View I want to render as a string is called by following Controller method:
public Task<IActionResult> IndexPartial(string? searchValue, string? filterValue)
{
IQueryable<Invoice> query
....Manipulate query by searchValue and filterValue
return PartialView("IndexPartial",await query.ToListAsync());
}
When I call Helper.RenderRazorViewToString(controller, "IndexPartial", model) I have to pass the stringValue and FilterValue.
Does Anybody know how to achieve this?
