.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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Qt 资料大全[通俗易懂]

    Qt 资料大全[通俗易懂]全网最强整理,Qt官网、编码风格、GitHub&Third-Party、社区论坛、博客、书籍等资源,应有尽有。

    2022年7月17日
  • Git的下载安装 (图文教程)

    Git的下载安装 (图文教程)git的下载安装目录git的下载git的安装步骤git的下载如果你还没有下载Git,可直接到git官网进行下载。如图直接按下Downloads选项这是页面会跳转到相应的页面按下Windows或DownloadforWindows选项,页面会自动跳转下载页面,当然其它系统类型根据需要下载相应的版本即可。下载完成双击运行点击运行…

    2022年5月27日
  • PyCharm下使用 ipython 交互式编程「建议收藏」

    PyCharm下使用 ipython 交互式编程「建议收藏」目的:方便调试,查看中间结果,因为觉得设断点调试相对麻烦。【运行环境:macOS10.13.3,PyCharm 2017.2.4】老手:选中代码行,Alt+Shift+E。或选中,鼠标右键-ExecuteLineinConsole。新手:1、安装ipython之后,查看 PyCharm 设置,确保Console的通用设置UseIPythonifavailable选项打勾。…

    2022年8月25日
  • 51单片机系列有哪些类型_51单片机1602液晶显示原理

    51单片机系列有哪些类型_51单片机1602液晶显示原理Lcd液晶屏幕带有背光显示,可选择蓝光白光黄光三种背光颜色的屏幕,它更具有美光性,在实际生活中有广泛的应用。实物图原理图RS:1为数据/0为指令;RW:1为读/0为写;E1为数据有效,下降沿执行指令示例代码main.c#include”reg52.h” //此文件中定义了单片机的一些特殊功能寄存器#include”lcd.h”typedefunsignedintu16; //对数据类型进行声明定义typedefunsignedcharu8;u8Disp

    2022年9月17日
  • Linux 查看环境变量_Linux怎么设置环境变量

    Linux 查看环境变量_Linux怎么设置环境变量各位客官好啊,最近新冠病毒疫情比较严重,还望各位出门时多多防范,一定要带口罩!!!接下来,讲一讲环境变量的相关知识点,既然提到了环境变量,那么我当前的环境里有多少默认的环境变量呢?关于这个问题,我教你两个命令就可以了,并且这两个命令一个比一个牛?先说一说【env】一、用env命令来查看环境变量上图就是我的系统输出的结果,简单给大家介绍几个重点的变量1)HOME:代表用户的家目录,通过cd…

    2022年9月1日
  • vue删除对象的某个属性(js怎么删除对象中的某个元素)

    微信小程序开发交流qq群173683895承接微信小程序开发。扫码加微信。实现代码:vardata={a:1,b:2,c:3}for(varitemindata){if(item==’b’){deletedata[item];}}console.log(‘data:…

    2022年4月11日

发表回复

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

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