zoukankan      html  css  js  c++  java
  • MVC4.0 JSON JsonResult 序列化之后 对JSON 进行修改 EXTJS4.0 分页

    事情是这样的:我在MVC 下 前后台交互 用JsonResult 返回给前台使用。

    public JsonResult AjaxFindHospitalInfo()
    {

      List<T> list = new List<T>

      return Json(list,JsonRequestBehavior.AllowGet);

    }

    但是在对EXTJS4.0 使用grid 使用分页功能的时候 需要把一个数据总行数一起写进JSON 

    {totalCount:" + 数据总行数+ ",bugs: " + JSON分页后的数据 + "}";

    要把数据总行数回传给组件。

    我想到几个办法:

    1. 改写LIST《T》 在list<t> 改为 list<list<T>> 最外层List 写入totalCount:  数据总行数,bugs : list<T>

     list<list<T>> =  {totalCount:  数据总行数,bugs : list<T>} 这样就可以直接通过"Json" 这个函数序列化

    2. 通过另外一个Ajax请求 取得 数据总行数,或 通过 ViewBag 传值,到前台 用 JS 取到 赋给 totalCount 

    事实上我用的第三种方法: ps:虽然我认为第二种方法 简单 方便

    3.取得Json 字符串 增加 字符串重新拼接成: {totalCount:" + 数据总行数+ ",bugs: " + JSON分页后的数据 + "}";

    但是Json 函数 返回的是JsonResult 没有函数能直接返回 字符串

    网上找方法:重写JsonResult 

    其中用到了这样一个  JavaScriptSerializer 这个对象有个函数 Serialize 可以把 List 序列化后返回 string

    通过 ContentResult 返回 Content(content),代码如下:

    public ContentResult AjaxFindHospitalInfo()
    {

    int start = Convert.ToInt32(Request.Params["start"] == null ? "0" : Request.Params["start"]);//从多少条开始
    int limit = Convert.ToInt32(Request.Params["limit"] == null ? "10" : Request.Params["limit"]);//分页页数

    Hospital_DAL hd = new Hospital_DAL();
    JavaScriptSerializer jss = new JavaScriptSerializer();
    string content = jss.Serialize(hd.FindHos(start, limit)); //hd.FindHos() 函数返回 LIST 集合

    int count = hd.FindHosCount();
    content = "{totalCount:" + count + ",bugs: " + content + "}";

    return Content(content);
    }

  • 相关阅读:
    Django模板
    创建我的第一个django项目
    python django环境配置与安装
    python第九期学习笔记(装饰器进阶)
    python第九期学习笔记(十二)(线程)
    python第九期学习笔记(装饰器)
    python第九期学习笔记(闭包)
    python第九期学习笔记(十二)(面向对象)
    python第九期学习笔记(十一)(面向对象)
    python第九期学习笔记(十)(模块)
  • 原文地址:https://www.cnblogs.com/90nice/p/3163770.html
Copyright © 2011-2022 走看看