栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

ASP.NET MVC-将Json结果与ViewResult结合

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ASP.NET MVC-将Json结果与ViewResult结合

您还可以将PartialViewResult呈现为字符串,然后通过JSON将该字符串传递给视图,并使用jQuery在页面中呈现。

您可以在这篇文章中看到:http : //www.atlanticbt.com/blog/asp-net-mvc-using-ajax-json-
and-partialviews/

我创建了一个扩展程序以使其更容易:

public static class MvcHelpers{    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();        }    }}

在我的控制器中,我将其称为:

const string msg = "Item succesfully updated!";return new JsonResult{    Data = new    {        success = true,         message = msg,        view = this.RenderPartialView("ProductItemForm", model)    },    JsonRequestBehavior = JsonRequestBehavior.AllowGet};

在这种情况下,“ this”是控制器,“ ProductItemForm”是我的视图,“ model”是我的productItem对象:)

希望这可以帮助 ;)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427588.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号