ASP.NET Core 6 MVC return partial view using Json

827 views Asked by At

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

There are 2 answers

3
Qing Guo On

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>');
                    }
0
Diego On

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;