方法一:使用ViewData赋值

首先,我们创建一个User的实体类

  1. public class User
  2. {
  3. public string UserID;
  4. public string UserName;
  5. public string Sex;
  6. }

然后,我们在Action中添加假数据,并将假数据放到ViewData中

  1. public ActionResult test()
  2. {
  3. List<User> listUser = new List<User>();
  4. listUser.Add(new User
  5. {
  6. UserID = "001",
  7. UserName = "呵呵",
  8. Sex = "男"
  9. });
  10. listUser.Add(new User
  11. {
  12. UserID = "002",
  13. UserName = "哈哈",
  14. Sex = "女"
  15. }); listUser.Add(new User
  16. {
  17. UserID = "003",
  18. UserName = "嘿嘿",
  19. Sex = "男"
  20. });
  21. ViewData["listUser"] = listUser;
  22. return View();
  23. }

最后,我们在前台用ViewData给DataGrid赋值

  1. <div>
  2. <table id="dg" class="easyui-datagrid" style="width: 600px; height: 300px" >
  3. <thead>
  4. <tr>
  5. <th data-options="field:'UserID',width:148,sortable:true">ID</th>
  6. <th data-options="field:'UserName',width:148,sortable:true">姓名</th>
  7. <th data-options="field:'Sex',width:148,sortable:true">性别</th>
  8. </tr>
  9. </thead>
  10. @foreach (ITOO.EvaluationUI.Models.User enUser in ViewData["listUser"] as List<ITOO.EvaluationUI.Models.User>)
  11. {
  12. <tr>
  13. <td>@enUser.UserID </td>
  14. <td>@enUser.UserName  </td>
  15. <td>@enUser.Sex  </td>
  16. </tr>
  17. }
  18. </table>
  19. </div>

方法二:使用url赋值

首先,我们在前台的DataGrid中加上URL属性

  1. <div>
  2. <table id="dg" class="easyui-datagrid" style="width: 600px; height: 300px" >
  3. <thead>
  4. <tr>
  5. <th data-options="field:'UserID',width:148,sortable:true">ID</th>
  6. <th data-options="field:'UserName',width:148,sortable:true">姓名</th>
  7. <th data-options="field:'Sex',width:148,sortable:true">性别</th>
  8. </tr>
  9. </thead>
  10. </table>
  11. </div>
  12. <!--datagrid基本设置-->
  13. <script type="text/javascript">
  14. $(function () {
  15. $('#dg').datagrid({
  16. title: '测试表格',
  17. url: "/EvaluationSituation/jsonTest",
  18. pagination: true,//显示分页工具栏
  19. });
  20. });
  21. </script>

然后,我们在相应的控制器中添加一个得到json数据的方法

  1. public JsonResult  jsonTest()
  2. {
  3. List<User> listUser = new List<User>();
  4. listUser.Add(new User {
  5. UserID ="001",
  6. UserName="呵呵",
  7. Sex ="男"
  8. });
  9. listUser.Add(new User
  10. {
  11. UserID = "002",
  12. UserName = "哈哈",
  13. Sex = "女"
  14. }); listUser.Add(new User
  15. {
  16. UserID = "003",
  17. UserName = "嘿嘿",
  18. Sex = "男"
  19. });
  20. JsonResult jsonUser = new JsonResult();
  21. jsonUser = Json(listUser);
  22. return jsonUser;
  23. }

上面介绍的两种方法能够解决我们给DataGrid赋值的问题,其中方法二里面除了将List集合转换成Json对象以外,我们还可以自己写一个方法将List转换成Json格式的字符串,这样也可以给DataGrid赋值。虽然我们能够赋值,但是这样做也有一些其他的问题,比如说怎么它的分页怎么实现,这就是下一节将要讲解的内容

EasyUI 之 DataGrid的两种赋值方法的更多相关文章

  1. Java中String对象两种赋值方式的区别

    本文修改于:https://www.zhihu.com/question/29884421/answer/113785601 前言:在java中,String有两种赋值方式,第一种是通过“字面量”赋值 ...

  2. 关于String的两种赋值方式

    String的两种赋值是不同的,String str1=“hello”,指向堆内存中的"hello",而String str2=new String("hello&quo ...

  3. 史上最全的CSS hack方式一览 jQuery 图片轮播的代码分离 JQuery中的动画 C#中Trim()、TrimStart()、TrimEnd()的用法 marquee 标签的使用详情 js鼠标事件 js添加遮罩层 页面上通过地址栏传值时出现乱码的两种解决方法 ref和out的区别在c#中 总结

    史上最全的CSS hack方式一览 2013年09月28日 15:57:08 阅读数:175473 做前端多年,虽然不是经常需要hack,但是我们经常会遇到各浏览器表现不一致的情况.基于此,某些情况我 ...

  4. bind()函数的深入理解及两种兼容方法分析

    在JavaScript中,bind()函数仅在IE9+.Firefox4+.Chrome.Safari5.1+可得到原生支持.本文将深入探讨bind()函数并对两种兼容方法进行分析比较.由于本文将反复 ...

  5. 菜鸡的Java笔记 第十三 String 类的两种实例化方法

    String 类的两种实例化方法 String 类的两种实例化方式的区别 String 类对象的比较 Stirng 类对象的使用分析 /*    1.String 类的两种实例化方式的区别       ...

  6. angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用

    今天我们要讲的是ng2的路由系统. 例子

  7. git两种合并方法 比较merge和rebase

    18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...

  8. easyui里弹窗的两种表现形式

    easyui里弹窗的两种表现形式 博客分类: jQueryEasyUi   1.主JSP页面中描绘弹窗   <div id="centerDiv" data-options= ...

  9. 两种Ajax方法

    两种Ajax方法 Ajax是一种用于快速创建动态网页的技术,他通过在后台与服务器进行少量的数据交换,可以实现网页的异步更新,不需要像传统网页那样重新加载页面也可以做到对网页的某部分作出更新,现在这项技 ...

随机推荐

  1. bzoj5157: [Tjoi2014]上升子序列(树状数组LIS)

    5157: [Tjoi2014]上升子序列 题目:传送门 题解: 学一下nlogn的树状数组求最长上生子序列就ok(%爆大佬) 离散化之后,用一个数组记录一下,直接树状数组做 吐槽:妈耶...一开始不 ...

  2. GitHub客户端和Shell的基本操作和理解

    GitHub客户端和Shell指令的简单实用 客户端操作, web端操作, shell指令操作. 掌握了这三种操作,基本上就可以很好的运用gitHub了. 创建项目, 可以通过web端进行创建. 可以 ...

  3. NOIP 2012 T2 国王游戏 (贪心+高精)

    思路: 呃呃网上那么多题解写得都不错-.. 就是高精 巨坑... 这里展出的是任氏高精(纯自己yy滴) //By SiriusRen #include <cstdio> #include ...

  4. (转载)打开一个本地apk进行安装

    1 2 3 4 5 6 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); File file = new File ...

  5. (转载)所有分类 > 开发语言与工具 > 移动开发 > Android开发 Android中的Service:默默的奉献者 (1)

    前言 这段时间在看一些IPC相关的东西,这里面就不可避免的要涉及到service,进程线程这些知识点,而且在研究的过程中我惊觉自己对这些东西的记忆已经开始有些模糊了——这可要不得.于是我就干脆花了点心 ...

  6. double int 类型的区别

    内部组织格式不同: po [NSString stringWithFormat:@"%d", f] 107886912 (lldb) po [NSString stringWith ...

  7. 手把手教你如何新建scrapy爬虫框架的第一个项目(上)

    前几天给大家分享了如何在Windows下创建网络爬虫虚拟环境及如何安装Scrapy,还有Scrapy安装过程中常见的问题总结及其对应的解决方法,感兴趣的小伙伴可以戳链接进去查看.关于Scrapy的介绍 ...

  8. Spring Framework 开发参考手册中文(在线HTML)

    https://blog.csdn.net/zfrong/article/details/3971722

  9. document.body

    比如document.body,最好是写成document.getElementsByTagName("body")[0];

  10. pyinstall 常见错误

    字符编码错误: https://blog.csdn.net/weixin_42426496/article/details/81102665 https://blog.csdn.net/qq_4206 ...