0

Is it possible to return 2 separate views to an ajax call in Asp.net?

for example, if foo1 and foo2 are 2 ActionResult methods each returning a view?

return Json(new { a = foo1(), b = foo2() });

currently attempting this, the end result is that javascript gets it back as a class object rather then the actual view, anybody know how to get the resulting rendered html instead?

EDIT: I guess what I'm actually going for, is there a way for me to return the rendered view in string format?

8
  • return Json returns just that (json), not html. You need to return a partial view. Commented Apr 19, 2015 at 1:54
  • the json itself could contain the html in string form, which is what I'm hoping to get, sorry, guess I framed my question a bit incorrectly. Commented Apr 19, 2015 at 1:56
  • You have some code. What about it is not functioning as you expected? Show actual output and expected output. Commented Apr 19, 2015 at 2:00
  • the code works perfectly fine, I could bypass what I'm trying to do, but what I want to do is cut multiple ajax calls into a single call, currently fooi and foo2 would be grabbed in 2 calls to the server, and I want it to be a single call. that involves figuring our how to return multiple views as json Commented Apr 19, 2015 at 2:03
  • That appears to be what your code is doing now. Which is why I don't understand what you're asking for here. Commented Apr 19, 2015 at 2:14

1 Answer 1

1

Yes their is a way of returning view in string format.

For this you need to do following things:

1.You need some method in which you can pass your viewname along with object model. For this please refer below code and add to your helper class.

 public static class RenderViewLib
        {
            public static string RenderPartialView(this Controller controller, string viewName, object model)
            {
                if (string.IsNullOrEmpty(viewName))
                    viewName = controller.ControllerContext.RouteData.GetRequiredString("action");

                controller.ViewData.Model = model;
                using (var sw = new StringWriter())
                {
                    ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                    var viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                    viewResult.View.Render(viewContext, sw);

                    return sw.GetStringBuilder().ToString();
                }
            }
        }

The above code will return your view in string format.

2.Now call above method from your json call like this:

 [HttpPost]
    public JsonResult GetData()
    {
        Mymodel model = new Mymodel();
        JsonResult jr = null;

        jr = Json(new
        {
            ViewHtml = this.RenderPartialView("_ViewName", model),
            ViewHtml2 = this.RenderPartialView("_ViewName2", model),
            IsSuccess = true
        }, JsonRequestBehavior.AllowGet);

        return jr;
    }

and you will get your view as string format from your json call.

Hope this is what you are looking for.

Sign up to request clarification or add additional context in comments.

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.