开发者

Need to return shared partial view in contentresult

开发者 https://www.devze.com 2023-01-19 03:29 出处:网络
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:

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.G开发者_Python百科etRequiredString("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.

0

精彩评论

暂无评论...
验证码 换一张
取 消