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


相关推荐

  • js的touch事件的实际引用

    一开始做前端页面的时候,接触的也是js,但是随后便被简单高效的jquery吸引过去,并一直使用至今。而js,则被我主观的认为底层技术而抛弃。直到这几天工作需要,研究移动端页面的触屏滑动事件,搜索jqu

    2021年12月27日
    56
  • asp.net MVC简单图片上传

    asp.net MVC简单图片上传asp.netMVC简单图片上传01、创建控制器HomeController.csusingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Mvc;namespacemvcTuPianShangChuang.Controllers{publicclassHomeController:Controller{

    2022年7月22日
    9
  • 如何完全禁止win10自动更新(自动升级)

    如何完全禁止win10自动更新(自动升级)一般来说,及时更新升级的操作系统是比较安全的。但是有的人对自动升级却非常讨厌。这里将介绍如何完全禁止win10自动升级。有多种方法,参照其一即可。转自:https://jingyan.baidu.com/article/1e5468f94dc9a3484961b7a8.html方法一(修改组策略)在cortana中输入gpedit.msc,打开通用管理文档在cortana中输入gpedit.m…

    2022年5月7日
    66
  • windows 批处理 注释_vim批量注释

    windows 批处理 注释_vim批量注释一、Windows批处理添加注释使用::即可注释文字如图所示

    2025年5月29日
    4
  • 解决busuanzi_count突然失效的方法(hexo-theme-next)

    解决busuanzi_count突然失效的方法(hexo-theme-next)

    2021年6月14日
    184
  • css控制滚动条透明,CSS控制滚动条样式的解析

    我们在之前的两篇文章中,我们给大家介绍了关于CSS设置div滚动条样式、以及CSS3自定义滚动条样式的实例,都知道当内容超出容器时,容器会出现滚动条,那我们如何使用CSS控制滚动条样式的呢?今天就给大家详细介绍!例子:/*作为IT界最前端的技术达人,页面上的每一个元素的样式我们都必须较真,就是滚动条我们也不会忽略。下面我给大家分享一下如何通过CSS来控制滚动条的样式,代码如下:*//*定义滚动条…

    2022年4月8日
    135

发表回复

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

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