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?

Kaylee
  • 145
  • 1
  • 10
  • `return Json` returns just that (`json`), not html. You need to return a partial view. –  Apr 19 '15 at 01: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. – Kaylee Apr 19 '15 at 01:56
  • You have some code. What about it is not functioning as you expected? Show actual output and expected output. – mason Apr 19 '15 at 02: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 – Kaylee Apr 19 '15 at 02:03
  • That appears to be what your code is doing now. Which is why I don't understand what you're asking for here. – mason Apr 19 '15 at 02:14
  • but it's not, what I get is the actual ActionView object. returning as View("viewname") and partialview also does the same thing. – Kaylee Apr 19 '15 at 02:20
  • Unclear what you hoping to achieve with this. Since ajax is async, 2 calls will probably give you better performance anyway. If you want to return a view, then return a partial containing the 2 partials returned by `foo1()` and `foo2()` –  Apr 19 '15 at 02:34
  • You can convert a view to string and return your json as yourself mentioned. Take a look at this answer for converting a view to string: http://stackoverflow.com/a/2759898/692422 – alisabzevari Apr 19 '15 at 06:44

1 Answers1

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.

Mukul Joshi
  • 166
  • 4