-1

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
  • You better include in your question an explanation what are you trying to reach? Why do you have to return partial view as a string when there are much better and easier ways? Commented Mar 15, 2023 at 23:04
  • HI, I said I want to render a Partial View, but it is render as a View instead... thanks Commented Mar 16, 2023 at 13:39

2 Answers 2

1

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>');
                    }
Sign up to request clarification or add additional context in comments.

3 Comments

HI Still it is render as View and not as Partial View. I think the problem is the function RenderViewToString... you are using the same I posted? Thanks
Hi @Diego , I try the both method and they render as Partial View. look: i.sstatic.net/0mazp.gif Do you add Layout = null; in the Partial View?
@Diego I see you "From Jquery I have this: $("#dvBody").html(result.url);" What's your return Json()? Can you try return Json(new { PartialView = viewContent }); like my code?
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.