简体   繁体   中英

How to return View with JSON result

Is there any way to return the View("controller", model) with JSON result? I've done like this(see code below) but it returns me an error.

    if (thereserror == true)
    {
        return Json(new
        {
            view = RenderRazorViewToString(ControllerContext, "Index", model),
            isValid = false,
            description = "Error!",
            JsonRequestBehavior.AllowGet
        });
    }
    else
    {
        return Json(new
        {
            view = RenderRazorViewToString(ControllerContext, "Index", model),
            isValid = true,
            description = "Hey!",
            JsonRequestBehavior.AllowGet
        });
    }

    private static string RenderRazorViewToString(ControllerContext controllerContext, string viewName, object model)
    {
        controllerContext.Controller.ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var ViewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
            var ViewContext = new ViewContext(controllerContext, ViewResult.View, controllerContext.Controller.ViewData, controllerContext.Controller.TempData, sw);
            ViewResult.View.Render(ViewContext, sw);
            ViewResult.ViewEngine.ReleaseView(controllerContext, ViewResult.View);
            return sw.GetStringBuilder().ToString();
        }
    }

For my AJAX I'm doing like this:

 $.ajax({
        type: "GET",
        url: "/serviceentry/getservice",
        data: ({ "SONumber": soNumber }),
        success: function (data) {
            if (data.isValid) {
                //I don't know what to put here
            };
        },
        error: function () {
            alert('error');
        }
    });

I saw something like this but I don't know what to do: MVC Return Partial View as JSON

JSON you are returning contains 4 properties, the way you are accessing isValid , similarly access the View .

$.ajax({
        type: "GET",
        url: "/serviceentry/getservice",
        data: ({ "SONumber": soNumber }),
        success: function (data) {
            if (data.isValid) {
                //Element- Where you want to show the partialView
                $(Element).html(data.view)
            };
        },
        error: function () {
            alert('error');
        }
    });

PS: Also Pointing the wrong placement of JSONRequestBehavior .

if (thereserror == true)
    {
        return Json(new
        {
            view = RenderRazorViewToString(ControllerContext, "Index", model),
            isValid = false,
            description = "Error!"
        },JsonRequestBehavior.AllowGet);
    }
    else
    {
        return Json(new
        {
            view = RenderRazorViewToString(ControllerContext, "Index", model),
            isValid = true,
            description = "Hey!"
        },JsonRequestBehavior.AllowGet);
    }

    private static string RenderRazorViewToString(ControllerContext controllerContext, string viewName, object model)
    {
        controllerContext.Controller.ViewData.Model = model;
        using (var sw = new StringWriter())
        {
            var ViewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
            var ViewContext = new ViewContext(controllerContext, ViewResult.View, controllerContext.Controller.ViewData, controllerContext.Controller.TempData, sw);
            ViewResult.View.Render(ViewContext, sw);
            ViewResult.ViewEngine.ReleaseView(controllerContext, ViewResult.View);
            return sw.GetStringBuilder().ToString();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM