作者:josh-jw

介绍

我们可以在web页面用HTML表格元素定义WebGrid显示数据,它以非常简单的方式呈现表格数据,支持自定义格式列,分页,排序,并通过AJAX异步更新。

WebGrid主要属性:

Source -数据来自哪里。 通常情况下,通过controller action传递model

DefaultSort -定义如何将数据排序。只要在这里提供列名。

RowsPerPage -每页表格显示的记录数。

CanPage -允许分页。

CanSort -允许通过点击列标题排序。

SelectedFieldName -获取查询字符串字段,用于指定所选行WebGrid实例的全名。

代码使用

在这篇文章中, MVC 4应用程序中使用WebGrid。 首先,我要创建一个名为Product的Model。

Product.cs

public class Product

{

public string Id { get; set; }

public string Name { get; set; }

public string Description { get; set; }

public long Quantity { get; set; }

}

我使用Razor视图引擎,InventoryController包含下面的action:

InventoryController.cs

public ActionResult WebgridSample()

{

ObservableCollection inventoryList =

new ObservableCollection();

inventoryList.Add(new Product { Id = "P101",

Name = "Computer", Description = "All type of computers", Quantity = 800 });

inventoryList.Add(new Product { Id = "P102",

Name = "Laptop", Description = "All models of Laptops", Quantity = 500 });

inventoryList.Add(new Product { Id = "P103",

Name = "Camera", Description = "Hd cameras", Quantity = 300 });

inventoryList.Add(new Product { Id = "P104",

Name = "Mobile", Description = "All Smartphones", Quantity = 450 });

inventoryList.Add(new Product { Id = "P105",

Name = "Notepad", Description = "All branded of notepads", Quantity = 670 });

inventoryList.Add(new Product { Id = "P106",

Name = "Harddisk", Description = "All type of Harddisk", Quantity = 1200 });

inventoryList.Add(new Product { Id = "P107",

Name = "PenDrive", Description = "All type of Pendrive", Quantity = 370 });

return View(inventoryList);

}

在WebgridSample视图里,我创建了WebGrid并在调用GetHtml时指定列。

WebgridSample.cshtml:

@{

var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5,

selectionFieldName: "selectedRow",ajaxUpdateContainerId: "gridContent");

grid.Pager(WebGridPagerModes.NextPrevious);

}

WebGrid helper允许我们添加页眉,页脚,行和交替行元素的样式。

.webGrid { margin: 4px; border-collapse: collapse; width: 500px; background-color:#FCFCFC;}

.header { background-color: #C1D4E6; font-weight: bold; color: #FFF; }

.webGrid th, .webGrid td { border: 1px solid #C0C0C0; padding: 5px; }

.alt { background-color: #E4E9F5; color: #000; }

.gridHead a:hover {text-decoration:underline;}

.description { width:auto}

.select{background-color: #389DF5}

添加列到表格中并指定列名、排序方式、字段绑定。

@grid.GetHtml(tableStyle: "webGrid",

headerStyle: "header",

alternatingRowStyle: "alt",

selectedRowStyle: "select",

columns: grid.Columns(

grid.Column("Id", format: (item) => item.GetSelectLink(item.Id)),

grid.Column("Name", " Name"),

grid.Column("Description", "Description", style: "description"),

grid.Column("Quantity", "Quantity")

))

为了浏览选定的项目,我使用了Id列的format参数。Oolumn方法的format参数,允许我们自定义数据项的渲染。

grid.Column("Id", format: (item) => item.GetSelectLink(item.Id))

下面的代码展示了如何以HTML代码方式显示选中的列,为此,我创建了一个Product模型实例。

@{

InventoryManagement.Models.Product product = new InventoryManagement.Models.Product();

}

@if (grid.HasSelection)

{

product = (InventoryManagement.Models.Product)grid.Rows[grid.SelectedIndex].Value;

Id @product.Id

Name @product.Name

Description @product.Description

Quantity @product.Quantity

}

为了避免页面分页时刷新,我们可以添加AJAX参数ajaxUpdateContainerId,包含网格的DIV标记。在这里,我们可以指定ajaxUpdateContainerId为div的id。

ajaxUpdateContainerId: "gridContent"

添加jQuery引用:

.net mvc html.row,ASP.NET MVC4中的WebGrid相关推荐

  1. mvc html.dropdownlist,ASP.NET MVC4中使用Html.DropDownListFor的方法示例

    本文实例讲述了ASP.NET MVC4中使用Html.DropDownListFor的方法.分享给大家供大家参考,具体如下: 一.控制器部分: public ActionResult PageDeta ...

  2. mvc4 html.dropdownlist,ASP.NET MVC4中使用Html.DropDownListFor的方法示例

    本文实例讲述了ASP.NET MVC4中使用Html.DropDownListFor的方法.分享给大家供大家参考,具体如下: 一.控制器部分: public ActionResult PageDeta ...

  3. ASP.NET MVC4中调用WEB API的四个方法

    当今的软件开发中,设计软件的服务并将其通过网络对外发布,让各种客户端去使用服务已经是十分普遍的做法.就.NET而言,目前提供了Remoting,WebService和WCF服务,这都能开发出功能十分强 ...

  4. ASP.NET MVC4中@model使用多个类型实例的方法

    有时需要在ASP.NET MVC4的视图的@model中使用多个类型的实例,.NET Framework 4.0版本引入的System.Tuple类可以轻松满足这个需求. 假设Person和Produ ...

  5. [转]ASP.NET MVC4中@model使用多个类型实例的方法

    本文转自:http://blog.csdn.net/hulihui/article/details/48199897 有时需要在ASP.NET MVC4的视图的@model中使用多个类型的实例,.NE ...

  6. ASP.NET MVC:利用ASP.NET MVC4的IBundleTransform集成LESS

    背景 LESS确实不错,只是每次写完LESS都要手工编译一下有点麻烦(VS插件一直没有安装好),昨天在官方看到了如何用IBundleTransform集成LESS,今天就记录一下. 参考资料:http ...

  7. 使用asp.net MVC4中的Bundle遇到的问题及解决办法

    背景 之前有过使用MVC3的经验,也建过MVC4的基本样例看过,知道有bundle这么一个方法. 近日想建个网站使用MVC4,但是我觉得在基本样例上改不好,有太多无用的东西,所以就建了一个空白的MVC ...

  8. ASP.NET MVC4中的异步控制器

    在抛弃了对.NET 3的支持之后, ASP.NET MVC 4 彻底拥抱了Task类库, 你不需要再蛋疼的给每个Action写两个方法, 也无需傻傻的手动对异步Action计数器增减了(AsyncMa ...

  9. ASP.NET MVC4中用 BundleCollection

    来源:http://www.cnblogs.com/madyina/p/3702314.html ASP.NET MVC4中对JS和CSS的引用又做了一次变化,在MVC3中我们这样引用资源文件: &l ...

  10. ASP.NET MVC 5 02 - ASP.NET MVC 1-5 各版本特点

    参考书籍:<ASP.NET MVC 4 高级编程>.<ASP.NET MVC 5 高级编程>.<C#高级编程(第8版)>.<使用ASP.NET MVC开发企业 ...

最新文章

  1. 飞机上一般是什么操作系统?
  2. SQL中ISNULL的问题。
  3. CRichEditCtrl 好象没有复制、粘贴的功能(Ctrl+C、Ctrl+v都不好用
  4. 中国最懂自动驾驶量产公司秀肌肉:自动驾驶算力怪兽、百亿参数云端超大模型、百万公里路测里程...
  5. linux 下用ecipse 作用oracle 的客户端
  6. Java实现将list数据取出并加入分隔符拼接,转换成String
  7. python调用ansysworkbench_Workbench通过Python设定材料参数
  8. 第三章 springboot + jedisCluster(转载)
  9. 简直要吐槽!!enable-migrations fails on x64 Projects
  10. django 中多字段主键(复合、联合主键)
  11. 中国数码相机镜头行业市场供需与战略研究报告
  12. mysql之创建数据库,创建数据表
  13. php网站微博帐号登录代码,微博登录按钮
  14. python调整excel列宽_python - 有没有一种方法可以使用pandas.ExcelWriter自动调整Excel列的宽度? - 堆栈内存溢出...
  15. Java筑基24-集合02-List
  16. 书籍推荐-docker入门书籍
  17. 第54件事 引爆流行的3个条件
  18. 逍遥模拟器过检测_王者荣耀怎么修改定位变更荣耀战区提升排位--逍遥模拟器电脑版更换荣耀战区...
  19. JS修改标签的css样式
  20. Boot与App程序设计

热门文章

  1. excel mysql乱码_excel打开是乱码的解法方法
  2. 教你视频去色效果怎么操作
  3. matlab 简单低通滤波器,基于MATLAB的理想低通滤波器的设计
  4. 安装MyBatis教程
  5. Spring boot 集成mybatis 教程
  6. 高德地图模仿微信发送位置实现拾取坐标
  7. plc用c语言编程的好处,学习PLC编程的重要性
  8. C# 使用微信扫码登录网页
  9. Axure RP9 的元件库
  10. sigar获取单个进程信息