Tip#1-使用扩展方法创建新的HTML Helper

该篇文章是Stephen Walther博客中的文章,个人觉得不错就翻译了一下。但由于个人英语水平有限,还望各位海量。如果想看原文,请点击ASP.NET MVC Tip #1 - Create New HTML Helpers with Extension Methods查看原文

这个提示里,我会展示给大家怎样创建能在Asp.net MVC view中应用的两个心得HTML Helpers。我会展示给大家通过使用扩展函数创建一个新的显示项目符号和数组列表的HTML Helpers

当你创建一个Asp.net MVC程序的View时,你可以使用利用HTML Helpers提供标准的HTML标记。例如,<input name="inpSubmit" type="submit" value="Click Here!" />的标记可以使用<%= Html.SubmitButton("inpSubmit", "Click Here!") %>代替。从长远方面看,使用HTML Helpers能够节省很多时间。但是如果现有的HTML Helpers中没有你要使用的的标记那该怎么办?例如,设想你要在View中以符号列表的形式显示数据库的记录集。现有的HtmlHelper中没有包含显示符号列表的方法。不要放弃,如果HtmlHelper中没有包含要使用的方法时,扩展一个就可以了。

你可以通过创建扩展函数添加新的功能性的HtmlHelper类。一个扩展方法看起来就像一个平常的实例化方法。不管怎样,和一般实例化方法不同,你向一个类中添加扩展方法通过在完全不同的类中定义方法。(这个句子有点不同,希望达人帮助解答一下However, unlike a normal instance method, you add extension methods to a class by defining the methods in a completely different class.

C#中,你定义扩展方法在一个静态类中并使用this关键字表明这个类被扩展。

这儿向HtmlHelper类添加扩展方法用于显示顺序或不按顺序排列数据集。

 1 using  System;
 2 using  System.Text;
 3 using  System.Web;
 4 using  System.Web.Mvc;
 5 using  System.Collections;
 6
 7 namespace  MvcAppTest.Helpers
 8 ExpandedBlockStart.gifContractedBlock.gif {
 9    public static class ListExtensions
10ExpandedSubBlockStart.gifContractedSubBlock.gif    {
11        public static string OrderedList(this HtmlHelper helper, Object items)
12ExpandedSubBlockStart.gifContractedSubBlock.gif        {
13            return "<ol>" + ListExtensions.GetListItems(items) + "</ol>";
14        }

15        public static string UnorderedList(this HtmlHelper helper, Object items)
16ExpandedSubBlockStart.gifContractedSubBlock.gif        {
17            return "<ul>" + ListExtensions.GetListItems(items) + "</ul>";
18        }

19        private static string GetListItems(Object items)
20ExpandedSubBlockStart.gifContractedSubBlock.gif        {
21            if (items == null)
22                throw new ArgumentException("items");
23            if (items is IEnumerable == false)
24                throw new InvalidCastException("items must be IEnumerable");
25
26            var enumItems = (IEnumerable)items;
27            var builder = new StringBuilder();
28            foreach(Object item in enumItems)
29ExpandedSubBlockStart.gifContractedSubBlock.gif            {
30                builder.AppendFormat("<li>{0}</li>", HttpUtility.HtmlEncode(item.ToString()));
31            }

32            return builder.ToString();
33        }

34    }

35}

 

ListExtentsions类有两个公共方法:OrderedList()UnorderedList()。你通过向任何一个方法传递一个items集合来显示一个按序和不按序排列的列表。注意这些方法返回的是string类型。实际上,HtmlHelper方法仅仅是向浏览器提供一个string格式化的函数。

在你创建一个扩展方法后,可在一个View页面中适用这个方法。

 

ContractedBlock.gif ExpandedBlockStart.gif Code
        <%=Html.ActionLink("excel1","GenerateExcel1"%><br />
         
<%=Html.ActionLink("excel2","GenerateExcel2"%><br />
          
<%=Html.ActionLink("excel3","GenerateExcel3"%>

 

注意项目符号类表的HtmlHelper的命名空间在文件的头部引入。Html.OrderedList()方法用于显示数字排序的列表,Html.UnorderedList()方法用于呈现符号类表。注意这些在HtmlHelper的方法就像其他的扩展方法一样都是通过View的Html属性暴露的。

1  private  CategoriesDataContext db  =   new  CategoriesDataContext();
2           public  ActionResult BulletedList()
3          {
4              var cat  =  from c  in  db.Categories select c.CategoryName;
5              ViewData[ " Catories " =  cat;
6              ViewData[ " Title " =   " BulletedList Test " ;
7               return  View();
8          }

你可以在其他任何的Asp.net MVCView页面中使用这个扩展函数。

转载于:https://www.cnblogs.com/Nimeux/archive/2008/08/19/1271361.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值