自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET MVC 3 Beta初體驗(yàn)之WebGrid

開(kāi)發(fā) 后端
在ASP.NET MVC 3 Beta發(fā)布后很多.NET程序員關(guān)心他的一些新功能。不過(guò)今天我們要講的,卻是WebGrid,將分別介紹在aspx視圖引擎和Razor視圖引擎中如何使用它

ASP.NET MVC 3 Beta中除了推出一種新的視圖引擎Razor。還推出了幾種新的HtmlHelper。我比較關(guān)注的是WebGrid,這篇文章將介紹一下WebGrid的使用。WebGrid提供了分頁(yè)和排序的功能,在此之前在MVC中分頁(yè)和排序時(shí)需要自己去寫(xiě)的。這篇文章將分別介紹在aspx視圖引擎和Razor視圖引擎中如何使用它。

我通過(guò)ADO.NET Entity Data Model從NORTHWND的Products中表中取數(shù)據(jù)。在Controller中取數(shù)據(jù):

  1. public class HomeController : Controller      
  2. {        public ActionResult Index()         
  3.  {             
  4.  NORTHWNDEntities entity = new NORTHWNDEntities();              
  5. return View( entity.Products.ToList());        
  6.   }     }  

在aspx視圖引擎中使用WebGrid代碼如下:

  1.  <div id="grid">   
  2. <% var grid = new WebGrid(source: Model, defaultSort: "ProductName", rowsPerPage: 5); %>   
  3. <%=grid.GetHtml(       
  4. tableStyle: "grid",       
  5. headerStyle: "head",       
  6. alternatingRowStyle: "alt",       
  7. columns: grid.Columns(                 
  8. grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ProductID })),                 
  9. grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", null, new { onclick = string.Format("deleteRecord('Employee', '{0}')", item.ProductID), @class = "Delete"href = "JavaScript:void(0)" })),                 
  10. grid.Column("ProductName","產(chǎn)品名稱(chēng)"),               
  11. grid.Column("QuantityPerUnit","每單位數(shù)量"),                 
  12. grid.Column("UnitPrice","單價(jià)"),               
  13. grid.Column("UnitsInStock", "庫(kù)存單位"),               
  14. grid.Column("UnitsOnOrder","訂單單位"),               
  15. grid.Column("ReorderLevel","重新排序級(jí)別"),               
  16. grid.Column("Discontinued","已停產(chǎn)")     )     )    %>
  17.  </div> 

在Razor中使用WebGrid代碼如下:

  1.  <div id="grid"> 
  2. @{    
  3. var grid = new WebGrid(source: Model,  
  4. defaultSort: "ProductName",   
  5. rowsPerPage: 10);  
  6. }  
  7. @grid.GetHtml(  
  8. tableStyle: "grid",  
  9. headerStyle: "head",  
  10. alternatingRowStyle: "alt",  
  11. columns: grid.Columns(  
  12. grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ProductID })),  
  13. grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", null, new { onclick = string.Format("deleteRecord('Product', '{0}')", item.ProductID), @class = "Delete"href = "JavaScript:void(0)" })),   
  14. grid.Column("ProductName","產(chǎn)品名稱(chēng)"),  
  15. grid.Column("QuantityPerUnit","每單位數(shù)量")  
  16. ,grid.Column("UnitPrice","單價(jià)"),  
  17. grid.Column("UnitsInStock", "庫(kù)存單位"),  
  18. grid.Column("UnitsOnOrder","訂單單位"),  
  19. grid.Column("ReorderLevel","重新排序級(jí)別"),  
  20. grid.Column("Discontinued","已停產(chǎn)")))</div> 

WebGrid構(gòu)造函數(shù)如下:

  1. public WebGrid(IEnumerable<dynamic> source, 
  2. IEnumerable<string> columnNames = null
  3. string defaultSort = null
  4. int rowsPerPage = 10, 
  5. bool canPage = truebool canSort = true
  6. string ajaxUpdateContainerId = null
  7. string fieldNamePrefix = null
  8. string pageFieldName = null
  9. string selectionFieldName = null
  10. string sortFieldName = null
  11. string sortDirectionFieldName = null);  
  12.  

常見(jiàn)參數(shù)意思是:

1、source 表示數(shù)據(jù)源

2、columnNames表示顯示的列

3、defaultSort 默認(rèn)按什么排序

4、rowsPerPage 每頁(yè)多少行數(shù)據(jù)

5、canPage 是否能排序

上面兩段代碼的意思是定義了一個(gè)既分頁(yè)又能排序的grid。

運(yùn)行:

在看看兩個(gè)view的完整代碼:

aspx:

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<List<WebGridAspx.Models.Products>>" %> 
  2. <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">      
  3. 產(chǎn)品列表  
  4. </asp:Content> 
  5. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
  6. <script  type="text/javascript">    
  7. function deleteRecord(a, b) {          
  8. alert("刪除:"+b);     
  9.  }</script> 
  10.  <h2>產(chǎn)品列表</h2>    
  11. <div id="grid">   
  12. <% var grid = new WebGrid(source: Model, defaultSort: "ProductName", rowsPerPage: 5); %>   
  13. <%=grid.GetHtml(       
  14. tableStyle: "grid",       
  15. headerStyle: "head",       
  16. alternatingRowStyle: "alt",       
  17. columns: grid.Columns(                
  18.  grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ProductID })),                 
  19. grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", null, new { onclick = string.Format("deleteRecord('Employee', '{0}')", item.ProductID), @class = "Delete"href = "JavaScript:void(0)" })),                 
  20. grid.Column("ProductName","產(chǎn)品名稱(chēng)"),               
  21. grid.Column("QuantityPerUnit","每單位數(shù)量"),                 
  22. grid.Column("UnitPrice","單價(jià)"),               
  23. grid.Column("UnitsInStock", "庫(kù)存單位"),               
  24. grid.Column("UnitsOnOrder","訂單單位"),               
  25. grid.Column("ReorderLevel","重新排序級(jí)別"),               
  26. grid.Column("Discontinued","已停產(chǎn)")     )     )    %> 
  27. </div></asp:Content> 

Razor:

代碼 

  1.  @model List<WebGridRazor.Models.Products> @{  View.Title = "產(chǎn)品列表"; } <p> 
  2. <h2>產(chǎn)品列表</h2>  <div id="grid">  @{       
  3. var grid = new WebGrid(source: Model,  defaultSort: "ProductName",   rowsPerPage: 3); }  @grid.GetHtml(       
  4. tableStyle: "grid",       
  5. headerStyle: "head",       
  6. alternatingRowStyle: "alt",       
  7. columns: grid.Columns(            
  8. grid.Column(format: (item) => Html.ActionLink("Edit", "Edit", new { id = item.ProductID })),          
  9. grid.Column(format: (item) => Html.ActionLink("Delete", "Delete", null, new { onclick = string.Format("deleteRecord('Product', '{0}')", item.ProductID), @class = "Delete"href = "JavaScript:void(0)" })),             
  10. grid.Column("ProductName","產(chǎn)品名稱(chēng)"),          
  11. grid.Column("QuantityPerUnit","每單位數(shù)量"),          
  12. grid.Column("UnitPrice","單價(jià)"),            
  13. grid.Column("UnitsInStock", "庫(kù)存單位"),          
  14. grid.Column("UnitsOnOrder","訂單單位"),          
  15. grid.Column("ReorderLevel","重新排序級(jí)別"),          
  16. grid.Column("Discontinued","已停產(chǎn)")     )     )   
  17. </div> </p> 

Razor去掉了那些模板頁(yè)的代碼,使代碼看起來(lái)更整潔。比較喜歡Razor。

總結(jié):本文很簡(jiǎn)單,介紹了一下ASP.NET MVC 3 Beta中新功能WebGrid,由于這種方式WebGrid是在內(nèi)存中分頁(yè)和排序的,所以不適合大數(shù)據(jù)量。

源碼下載地址:http://down.51cto.com/data/134994

原文標(biāo)題:ASP.NET MVC 3 Beta之WebGrid的使用

鏈接:http://www.cnblogs.com/zhuqil/archive/2010/10/17/ASP-NET-MVC-3-Beta-WebGrid.html

【編輯推薦】

  1. 詳解ASP.NET MVC 3 beta新特性
  2. .Net平臺(tái)下的分布式緩存設(shè)計(jì)
  3. .Net平臺(tái)開(kāi)源項(xiàng)目五年發(fā)展回顧
  4. .NET平臺(tái)上Web開(kāi)發(fā)的未來(lái)?
  5. .NET開(kāi)發(fā)人員應(yīng)該關(guān)注的七個(gè)開(kāi)源項(xiàng)目
責(zé)任編輯:彭凡 來(lái)源: 博客園
相關(guān)推薦

2010-10-12 09:52:02

ASP.NET MVC

2010-10-20 09:05:16

ASP.NET MVC

2009-07-20 16:44:56

ASP.NET MVCIValueProvi

2009-07-23 10:08:24

asp.net mvc

2010-12-07 09:38:15

ASP.NET MVC

2010-06-23 15:44:03

ASP.NET MVC

2010-06-25 08:51:46

ASP.NET MVC

2014-07-29 10:00:30

ASP.NETMVCAutoFac

2009-07-24 13:20:44

MVC框架ASP.NET

2009-07-31 12:43:59

ASP.NET MVC

2011-09-28 13:48:06

Visual Stud

2011-01-15 23:07:59

2009-07-28 17:17:19

ASP.NET概述

2009-07-23 15:44:39

ASP.NET MVC

2009-07-22 10:09:59

ASP.NET MVC

2009-07-22 13:24:24

ASP.NET MVC

2009-07-20 10:53:59

ASP.NET MVC

2009-07-23 14:31:20

ASP.NET MVC

2011-01-28 09:45:29

ASP.NET MVC

2011-04-14 09:19:22

ASP.NET MVC
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)