方法一:使用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. 在GitHub上使用Hexo搭建静态博客

    搭建静态博客须要一个前提是电脑上有安装git而且有github帐号,这个不懂能够看廖雪峰先生的git教程 1.下载nodejs.在官网上能够下载 2.使用git进入你新建的一个目录,输入命令 npm ...

  2. linux操作系统下完全删除oracle数据库

    1.关掉oracle server 和 background processes ps -ef | grep ora 关掉数据库 shutdown immediate 2.关掉监听 lsnrctl  ...

  3. SpringMVC+Spring+Hibernate框架整合原理,作用及使用方法

    转自:https://blog.csdn.net/bieleyang/article/details/77862042 SSM框架是spring MVC ,spring和mybatis框架的整合,是标 ...

  4. gist.github.com

    hosts添加:192.30.253.118 gist.github.com

  5. ios问题bug收录——1

     **[2713:59682] *** Assertion failure in -[MBProgressHUD show:], /Users/lu/Desktop/****/Pods/MBProgr ...

  6. Bringing up the Avnet MicroZed with Vivado

    Bringing up the Avnet MicroZed with Vivado I recently received the Adam Taylor Edition of Avnet's Zy ...

  7. sql server Delete误操作后如何恢复数据

    声明:本文是根据别人的经验https://blog.csdn.net/dba_huangzj/article/details/8491327写的总结 说明:update和delete时没有加where ...

  8. 如何在Windows下安装Linux子系统(Ubuntu,openSUSU,SUSU Linux Server)

    注意:只有win10才能安装,安装的linux没有图形界面. 1.首先在win10设置 --> 更新与安装 --> 针对开发人员 ,选择开发人员模式. 2.win10 Cortana -- ...

  9. c/s结构的自动化——pyautogui

    环境:Python 3.5.3 pip install pyautogui -i http://pypi.douban.com/simple --trusted-host pypi.douban.co ...

  10. javascript 继承之拷贝,原型,类式

    // 拷贝继承,在子类内调用父类并修正this指向,再通过for in 拷贝父类的方法实现继承,具体实现如下代码 : function Tab(){//父类构造函数 this.name='aaa'; ...