.net mvc5_mvc工作流程

.net mvc5_mvc工作流程作者:josh-jw介绍我们可以在web页面用HTML表格元素定义WebGrid显示数据,它以非常简单的方式呈现表格数据,支持自定义格式列,分页,排序,并通过AJAX异步更新。WebGrid主要属性:Source-数据来自哪里。通常情况下,通过controlleraction传递modelDefaultSort-定义如何将数据排序。只要在这里提供列名。RowsPerPage-每页表格显示…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

作者: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引用:

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186929.html原文链接:https://javaforall.net

(0)
上一篇 2022年10月1日 下午11:36
下一篇 2022年10月1日 下午11:36


相关推荐

  • echarts柱状图下钻

    echarts柱状图下钻DOCTYPE tml htmlstyle quot height 100 quot head metacharset quot utf 8 quot metacharset quot utf 8 quot head htmlstyle quot height 100 quot

    2026年3月16日
    2
  • Linux时间戳与日期之间的转换

    Linux时间戳与日期之间的转换背景由于 Linux 系统中 我们经常需要相关时间戳信息 而默认的时间戳信息是从 1970 年 1 月 1 日 UTC GMT 的午夜 开始所经过的秒数 UNIX 时间戳的 0 按照 ISO8601 规范为 1970 01 01T00 00 00Z 一个小时表示为 UNIX 时间戳格式为 3600 秒 一天表示为 UNIX 时间戳为 86400 秒 闰秒不计算 所以 我们需要做下转换 才是我们所能理解的正常日期 转换方式

    2026年3月17日
    2
  • Matlab 归一化函数premnmx

    Matlab 归一化函数premnmx函数目的是把数据处理成 1 1 之间 算法是 如 a 2 4 3 5 那么计算过程就是 2 2 2 5 2 1 1 2 4 2 5 2 1 1 3 0 6666 2 3 2 5 2 1 0 66662 5 2 5 2 1 1

    2026年3月19日
    2
  • 【交换机】MAC-VLAN的功能作用是什么,如何设置[通俗易懂]

    【交换机】MAC-VLAN的功能作用是什么,如何设置[通俗易懂]MACVLAN就是基于MAC地址划分的VLAN,MACVLAN的最大优点就是用户不需要固定在某些端口下,可以随意移动,比如当用户物理位置移动时,即从一台交换机换到其它的交换机时,VLAN不用重新配置,所以,可以认为这种根据MAC地址的划分VLAN方法是基于用户的MAC地址信息来的。MACVLAN的缺点是初始化时,所有的用户都必须进行配置MAC与VLAN的对应关系。主要有两个配置步骤:1、所

    2022年8月10日
    22
  • PHP一句话木马使用技巧

    PHP一句话木马使用技巧近来发现好多网站有安全狗,会拦截菜刀和蚁剑,因此总结下通过浏览器直接调用一句话木马的姿势。PHP一句话:<?php@eval($_POST[‘shy’]);?>firefox浏览器:hackbar命令提示符:curl首先上传一句话木马到网站目录,可以是虚拟机也可以是VPS,我这里用的是阿里云我自己搭建的网站,由于只是简单的一句话木马,因次一上车就收到了安全云的短信…

    2022年5月21日
    98
  • Discord Bot开发:实现交互式问卷并有效收集用户文本回复

    Discord Bot开发:实现交互式问卷并有效收集用户文本回复

    2026年3月12日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号