WebGrid 在asp.net mvc中的使用和理解(译)

WebGrid 在asp.net mvc中的使用和理解(译)1:思路webgrid就是表格,一行行记录,代表一个个模型,因此,我们只需要在models文件夹建立模型,在控制器生成模型列表,把列表作为模型传入视图(或者绑定强类型视图,这个类型至少大于等于此模型列

大家好,又见面了,我是你们的朋友全栈君。

<span role="heading" aria-level="2">WebGrid 在asp.net mvc中的使用和理解(译)1:思路

webgrid就是表格,一行行记录,代表一个个模型,因此,我们只需要在models文件夹建立模型,在控制器生成模型列表,把列表作为模型传入视图(或者绑定强类型视图,这个类型至少大于等于此模型列表),然后通过GetHtml(Model,propetiesList)即可. 非常简单.

下面对webgrid的一些属性和方法进行介绍(这在设置表格的行列字段属性分页排序等特别适用)

AjaxUpdateCallback:异步操作完成后的回调函数

AjaxUpdateContainerId:更新所包括的元素

CanSort:是否支持排序

ColumnNames:列的名称列表,类似数据库字段列表,通常我们使用List<string>类型

HasSelection:判定行的选中

IsAjaxEnabled Returns a value that indicates whether the WebGrid instance can use Ajax calls to refresh the display

类似还有行数,默认选中,分页,每页多少行等等属性,具体参考msdn即可.

 请参考原文,感谢作者.

通过和原文比较,我觉得我的做法也许更好(原文使用的是弱类型视图,然后在视图中去new 模型或模型列表对象)

我是使用强类型视图,action可以直接传过来,viewbag也可以传过来,视图中也可以new

cshtml文件如下:

@{     Layout = null; }

<!DOCTYPE html>

<html> <head>     <title>WebgridSample</title>     <script src=”../../Scripts/jquery-1.7.1.min.js” type=”text/javascript”></script>     <style type=”text/css”>         .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}     </style> </head> <body> @{     WebGridSampleApplication.Models.Product product = new WebGridSampleApplication.Models.Product(); }

 @{
    var grid = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: “selectedRow”,ajaxUpdateContainerId: “gridContent”);
        grid.Pager(WebGridPagerModes.NextPrevious);}
        <div id=”gridContent”>
        @grid.GetHtml(tableStyle: “webGrid”,
                headerStyle: “header”,
                alternatingRowStyle: “alt”,
                selectedRowStyle: “select”,
                columns: grid.Columns(
                grid.Column(“Id”, format: (i) => i.GetSelectLink(i.Id)),
                         grid.Column(“Name”, ” Name”, (i) => i.GetSelectLink(i.Name)),
                grid.Column(“Description”, “Description”, style: “description”),
                grid.Column(“Quantity”, “Quantity”)
                  ))

 @if (grid.HasSelection)          {              product = (WebGridSampleApplication.Models.Product)grid.Rows[grid.SelectedIndex].Value;              <b>Id</b> @product.Id<br />              <b>Name</b>  @product.Name<br />              <b>Description</b> @product.Description<br />              <b>Quantity</b> @product.Quantity<br />          }     </div>     </body> </html>

 

模型文件如下:

using System; using System.Collections.Generic; using System.Linq; using System.Web;

namespace WebGridSampleApplication.Models {     public class Product     {         public string Id { get; set; }         public string Name { get; set; }         public string Description { get; set; }         public long Quantity { get; set; }     } }

 

控制器文件如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Collections.ObjectModel;
using WebGridSampleApplication.Models;

namespace WebGridSampleApplication.Controllers {     public class InventoryController : Controller     {         public ActionResult WebgridSample()         {

            ObservableCollection<Product> inventoryList = new ObservableCollection<Product>();             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);
        }
    }
}

hope this would help you

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java 16进制数据格式化处理工具类,16进制byte数组转String

    java 16进制数据格式化处理工具类,16进制byte数组转String废话不多说,直接上代码。格式化16进制字节数组,做数据处理时用得着,收藏吧!

    2022年8月30日
    3
  • 搭建备份服务器

    搭建备份服务器一、rsync的特性rsync的特性:1、支持拷贝特殊文件,设备等2、可以有排除指定文件或者目录同步的功能,相当于tar的排除功能3、可以做到保持源文件或目录的权限、时间、软硬连接、属主

    2022年7月3日
    23
  • [代码与实例] 爬虫 爬ooxx图 嘿嘿

    [代码与实例] 爬虫 爬ooxx图 嘿嘿闲话不多说直接撸代码importurllib.requestimporturllib.errorimportosimportsysimporthttp.serverimporthttp.clientimporttimeimportreimportrandomimportmathdata=Noneheaders={‘User-Agent’:’Mozilla/5.0(WindowsNT6.1;WOW64)AppleWebKit/537.36(

    2025年8月10日
    5
  • 用户态和内核态的区别线程切换_用户空间和内核空间的区别

    用户态和内核态的区别线程切换_用户空间和内核空间的区别内核态:cpu可以访问内存的所有数据,包括外围设备,例如硬盘,网卡,cpu也可以将自己从一个程序切换到另一个程序。用户态:只能受限的访问内存,且bu

    2022年9月18日
    2
  • matlab读取h5文件变慢_matlab载入数据文件

    matlab读取h5文件变慢_matlab载入数据文件filename=’C:\Users\88304\Desktop\Retina-Unet-1\test\test_last_weights.h5′;h5disp(filename)

    2025年8月27日
    6
  • pad图和n-s图_N S W

    pad图和n-s图_N S W(未完成_N-S图、PAD图概念未写)1、记录StudentRecord给出即将大学毕业的学生的姓名和平均分(GPA)。我们的目的是建立一个参加毕业典礼的学生表。候选毕业的学生表从文件”StudRecs”读入。因为学校规定:GPA低于minGPA的学生不能毕业,因此那些平均分低于minGPA的学生不参加毕业典礼。另外,记录那些选择不参加毕业典礼的学生名单,将名单保存至文件”NoAttend”中,按每行一个学生姓名保存。删除这些选择不参加毕业典礼的学生,最终生成参加毕业典礼的学生表。2、画出下列程序流

    2022年8月13日
    4

发表回复

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

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