It is necessary to return the general partial view to contentresult

I created a basecontroller class based on a blog post that I found to return a partial view as a result of an action of type ContentResult in my controller. The code for this 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 this:

return Content(RenderPartialViewToString("LocationStaffSearch", lcps));

So, now I need to return a partial, that general view. I can’t figure out how to pass a name to find a partial one. If I just type the name, it displays an empty string. If I put Share / LocationStaffSearch, it will return an error stating that the view is null.

+3
source share
1 answer

, - , . , . , , , .

+2

Source: https://habr.com/ru/post/1767921/


All Articles