[翻譯] ASP.NET MVC Tip #8 – 建立ASP.NET MVC GridView輔助方法

來源:互聯網
上載者:User
ASP.NET MVC Tip #8 – 建立ASP.NET MVC GridView輔助方法

原文地址:http://weblogs.asp.net/stephenwalther/archive/2008/06/24/asp-net-mvc-tip-8-create-an-asp-net-mvc-gridview-helper-method.aspx

翻譯:Anders Liu

摘要:在這個Tip中,你將學到如何擴充ASP.NET MVC架構,建立一個新的輔助方法,可以為資料庫資料顯示一個HTML表格。

目前,ASP.NET MVC架構還沒有包含任何直接等價於ASP.NET Web Forms GridView控制項的東西。每次你需要顯示資料時,你都必須編寫所有的HTML和內聯代碼。在這個Tip中,我將向你展示圖和為HtmlHelper類添加一個GridView()擴充方法。

擴充方法是由一個類為另一個類添加的方法。你可以使用擴充方法來為現有的類添加更多的功能。在這裡,我們將為HtmlHelper類——你在MVC視圖頁中常用的類——添加一個新的GridView()方法,用於為資料庫資料呈現一個HTML表格。在Visual Basic .NET和C#中實現擴充方法有些不同。在Visual Basic .NET中建立擴充方法需要建立一個模組,然後用為模組中的函數添加特性。在C#中建立擴充方法需要建立一個靜態類,並使用this關鍵字聲明該靜態類重的每個擴充方法的第一個參數。

清單1列出了GridView()擴充方法的代碼。

清單1 - GridExtensions.cs

<br />using System;<br />using System.Text;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Data.Linq.Mapping;<br />using System.Data.Linq;<br />using System.Web.UI;<br />using System.Web.Mvc;<br />using System.Web;</p><p>namespace Tip8.Helpers<br />{<br /> public static class GridExtensions<br /> {<br /> public static string GridView(this HtmlHelper htmlHelper, ITable table)<br /> {<br /> return GridView(htmlHelper, table, null, new GridViewOptions());<br /> }</p><p> public static string GridView(this HtmlHelper htmlHelper, ITable table, string[] headers)<br /> {<br /> return GridView(htmlHelper, table, headers, new GridViewOptions());<br /> }</p><p> public static string GridView(this HtmlHelper htmlHelper, ITable table, bool includeLinks)<br /> {<br /> return GridView(htmlHelper, table, null, includeLinks);<br /> }</p><p> public static string GridView(this HtmlHelper htmlHelper, ITable table, string[] headers, bool includeLinks)<br /> {<br /> var options = new GridViewOptions();<br /> if (!includeLinks)<br /> {<br /> options.ShowViewButton = false;<br /> options.ShowEditButton = false;<br /> options.ShowDeleteButton = false;<br /> }<br /> return GridView(htmlHelper, table, null, options);<br /> }</p><p> public static string GridView(this HtmlHelper htmlHelper, ITable table, string[] headers, GridViewOptions options)<br /> {<br /> // Show edit column?<br /> bool showEditColumn = options.ShowViewButton || options.ShowEditButton || options.ShowDeleteButton; </p><p> // Get identity column name<br /> string identityColumnName = GridExtensions.GetIdentityColumnName(table);</p><p> // Get column names and headers<br /> var columnNames = GridExtensions.GetColumnNames(table);<br /> if (headers == null)<br /> headers = columnNames;</p><p> // Open table<br /> var sb = new StringBuilder();<br /> sb.AppendLine("<br /><table>");</p><p> // Create Header Row<br /> sb.AppendLine("<thead>");<br /> sb.AppendLine("<br /><tr>");<br /> if (showEditColumn)<br /> sb.Append("<th></th><p>");<br /> foreach (String header in headers)<br /> sb.AppendFormat("<th>{0}</th><p>", header);<br /> sb.AppendLine("</tr><p>");<br /> sb.AppendLine("</thead><p>");</p><p> // Create Data Rows<br /> sb.AppendLine("<br /><tbody>");<br /> sb.AppendLine("<br /><tr>");<br /> foreach (Object row in table)<br /> {<br /> if (showEditColumn)<br /> {<br /> int identityValue = (int)DataBinder.GetPropertyValue(row, identityColumnName);<br /> sb.Append("<td><small>");<br /> if (options.ShowViewButton)<br /> {<br /> sb.Append(htmlHelper.ActionLink(options.ViewButtonText, options.ViewAction, new { Id = identityValue }));<br /> sb.Append(" ");<br /> }<br /> if (options.ShowEditButton)<br /> {<br /> sb.Append(htmlHelper.ActionLink(options.EditButtonText, options.EditAction, new { Id = identityValue }));<br /> sb.Append(" ");<br /> }<br /> if (options.ShowDeleteButton)<br /> {<br /> sb.Append(htmlHelper.ActionLink(options.DeleteButtonText, options.DeleteAction, new { Id = identityValue }));<br /> }<br /> sb.Append("</small></td><p>");<br /> }<br /> foreach (string columnName in columnNames)<br /> {<br /> string value = DataBinder.GetPropertyValue(row, columnName).ToString();<br /> sb.AppendFormat("<td>{0}</td><p>", HttpUtility.HtmlEncode(value));<br /> }<br /> sb.AppendLine("</tr><p>");<br /> }<br /> sb.AppendLine("</tbody><p>");</p><p> sb.AppendLine("</table><p>");<br /> return sb.ToString();<br /> }</p><p> public static string[] GetColumnNames(ITable table)<br /> {<br /> return table<br /> .Context<br /> .Mapping<br /> .GetMetaType(table.ElementType)<br /> .PersistentDataMembers.Select(m => m.Name)<br /> .ToArray();<br /> }</p><p> public static string GetIdentityColumnName(ITable table)<br /> {<br /> return table<br /> .Context<br /> .Mapping<br /> .GetMetaType(table.ElementType)<br /> .DBGeneratedIdentityMember<br /> .Name;<br /> }<br /> }<br />}<br />

清單1包含了GridView()方法的多個版本。每個版本的GridView()方法都接受一組不同的參數。例如,第一個版本的GridView()方法接受一個LINQ to SQL表,並呈現來自該表的所有列和行。其它版本的GridView()方法允許你自訂GridView的表頭和編輯連結。

清單2中的MVC視圖展示了多種調用GridView()方法來展示資料表內容的方式。

清單2 - Index.aspx

<content id="indexContent" contentplaceholderid="MainContent" runat="server"><br />Simple GridSimple Grid without LinksSimple Grid with Custom HeadersSimple Grid with Custom Links<p></content>

清單2中的檢視窗產生了圖1所示的HTML頁。該頁麵包含四個單獨的資料格(圖中只顯示了前三個)。

圖1 - Index視圖

注意ViewData.Model被傳遞到GridView()輔助方法中了。ViewData.Model包含了一個LINQ to SQL表。Index視圖的code-behind檔案為model指定了強型別System.Data.Linq.ITable。model是通過清單3所示的控制器代碼傳遞到視圖中的。

清單3 - HomeController.cs

<br />using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Web;<br />using System.Web.Mvc;<br />using Tip8.Models;</p><p>namespace Tip8.Controllers<br />{<br /> public class HomeController : Controller<br /> {<br /> private MovieDataContext _db = new MovieDataContext();</p><p> public ActionResult Index()<br /> {<br /> return View(_db.Movies);<br /> }<br /> }<br />}<br />

我對該Tip中的這個GridView()輔助方法並不是非常滿意。其問題在於使用擴充方法很難自訂GridView中列的外觀。例如,我可能希望能夠格式化現金和日期列。如果能夠有與模板列等價的功能就更好了。在明天的Tip中,我將介紹在使用ASP.NET MVC架構時,一種完全不同的封裝GridView的方法。

此處下載原始碼:http://weblogs.asp.net/blogs/stephenwalther/Downloads/Tip8/Tip8.zip。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.