skip to Main Content

I am writing an ASP.NET Core 6 MVC application.

I have a method in a controller that I need to retrieve a Json value like this

a boolean and a Partial View with the model class

return Json(new { success = true, PartialView("SearchResult", resultViewModel) });

I found this article that says to retrieve PartialView as string. [here][1]

like this

return Json(new { error = true, message = RenderViewToString(PartialView("Evil", model))});

I dont have RenderViewToString function. Searching.. I found this

protected string RenderViewAsString( string viewName, object model)
        {
            viewName = viewName ?? ControllerContext.ActionDescriptor.ActionName;
            ViewData.Model = model;

            using (StringWriter sw = new StringWriter())
            {
                IView view = _viewEngine.FindView(ControllerContext, viewName, true).View;
                if(view!=null)
                { 
                    ViewContext viewContext = new ViewContext(ControllerContext, view, ViewData, TempData, sw, new HtmlHelperOptions());
                    view.RenderAsync(viewContext).Wait();
                }
                return sw.GetStringBuilder().ToString();
            }
        }

From Jquery I have this

  success: function (result) {
                        $("#dvBody").html(result.url);
                    },

the problem is that it returns a View instead of a PartialView

Is there a way I can make it return a PartialView?

thanks
[1]: MVC Return Partial View as JSON

2

Answers


  1. Chosen as BEST ANSWER

    I replace this function

    protected string RenderViewAsString( string viewName, object model)
            {
                viewName = viewName ?? ControllerContext.ActionDescriptor.ActionName;
                ViewData.Model = model;
    
                using (StringWriter sw = new StringWriter())
                {
                    IView view = _viewEngine.FindView(ControllerContext, viewName, true).View;
                    if(view!=null)
                    { 
                        ViewContext viewContext = new ViewContext(ControllerContext, view, ViewData, TempData, sw, new HtmlHelperOptions());
                        view.RenderAsync(viewContext).Wait();
                    }
                    return sw.GetStringBuilder().ToString();
                }
            }
    

    for this one

     public string ConvertViewToString(ControllerContext controllerContext, PartialViewResult pvr, ICompositeViewEngine _viewEngine)
            {
                using (StringWriter writer = new StringWriter())
                {
                    ViewEngineResult vResult = _viewEngine.FindView(controllerContext, pvr.ViewName, false);
                    ViewContext viewContext = new ViewContext(controllerContext, vResult.View, pvr.ViewData, pvr.TempData, writer, new HtmlHelperOptions());
    
                    vResult.View.RenderAsync(viewContext);
    
                    return writer.GetStringBuilder().ToString();
                }
            }
    

    I think there is problem in this line

    IView view = _viewEngine.FindView(ControllerContext, viewName, true).View;
    

  2. the problem is that it returns a View instead of a PartialView

    Try to add below in your PartialView:

    @{
        Layout = null;
    }
    

    result:
    enter image description here

    My demo like below:

            [HttpPost]
            public IActionResult Details(string customerId)
            {
                Phone phone = GetCustomers().Where(x => x.Id == Convert.ToInt32(customerId)).FirstOrDefault().Phone;
                PartialViewResult partialViewResult = PartialView("_Phone", phone);
               
                string viewContent = RenderViewToString("_Phone", phone);
    
                return Json(new { PartialView = viewContent });
            }
    

    ajax success function:

    success: function (response) {                       
                            $('#dialog').append('<p>' + response.partialView + '</p>');
                        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search