I've created a basecontroller class based of a blog post I found to return a partial view as a result in a ContentResult type action in my controller. The code for that is here:
protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
ViewData.Model = model;
using (StringWriter sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
I use the following to return that:
return Content(RenderPartialViewToString("LocationStaffSearch", lcps));
So now I need to return a partial that's a shared view. I can't seem to figure out how to pass the name so it finds the partial. If I just type in the name it renders out a blank string. If I put in Share/LocationStaffSearch it returns an error saying the view is null.
I found the answer, the problem was at some point in the past I had created another partial view with the same name in the controller's view folder. It found that one first and rendered it. Once I took that one out and the shared one was the only one that existed it rendered fine.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With