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


相关推荐

  • 网站10大常见安全漏洞及解决方案

    网站10大常见安全漏洞及解决方案

    2021年10月31日
    42
  • 矩阵特征值分解(EDV)与奇异值分解(SVD)在机器学习中的应用

    目录特征分解定义(来自百度百科词条:特征分解)特征分解(Eigendecomposition),又称谱分解(Spectraldecomposition)是将矩阵分解为由其特征值和特征向量表示的矩阵之积的方法。需要注意只有对可对角化矩阵才可以施以特征分解。(来自百度百科词条:矩阵特征值)什么是特征值,特征向量?设A是n阶方阵,如果数λ和n维非零列向量x使关系式Ax=λx成立,那么这样的数λ称为矩阵A特征值,非零向量x称为A的对应于特征值λ的特征向量。式Ax=λx也可写成(A-λE)X=0。这是

    2022年4月5日
    46
  • pandas读取excel文件,转换为字典

    pandas读取excel文件,转换为字典pandas读取excel文件,转换为字典

    2025年6月9日
    0
  • python百度文库怎么免费下载文档(python编程是啥)

    每天早晨8点50分,准点开车打卡今天跟大家推荐一个简单实用的下载百度积分文档方法。虽然现在很多人都很少用百度文库中的东西了,不过后台还是经常收到一些读者留言,求帮忙下载百度文档或者请教如何才能免下载券下载。其实下载百度文库的方法,我很久之前就推荐过给大家了,可能你不记得了,推荐个免费的积分文档下载神器。这篇文章是推荐你使用冰点下载器,就可以自由下载百度,豆丁,畅享,mbalib,hp009,…

    2022年4月13日
    35
  • crontab定时任务不执行的原因

    crontab定时任务不执行的原因我在网上找的时候发现网上主要说了这5个原因:1crond服务未启动crontab不是Linux内核的功能,而是依赖一个crond服务,这个服务可以启动当然也可以停止。如果停止了就无法执行任何定时任务了,解决的方法是打开它:crond或servicecrondstart如果提示crond命令不存在,可能被误删除了,CentOS下可以通过这个命令重新安装:yum…

    2022年7月17日
    35

发表回复

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

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