4月2

向HtmlHelper类中添加自定义的方法

Large | Medium | Small
20:18 ASP.NET » ASP.NET MVC  From: Original
Reads(8036) | Comments(0) | Trackbacks(0)

 

新建HtmlHelpers类

image

添加如下代码

using System.Web.Mvc;  

namespace MvcMusicStore.Helpers

{    

    public static class HtmlHelpers    

    {        

        public static string Truncate(this HtmlHelper helper  , string input, int length)        

        {            

            if (input.Length <= length)            

            {                

                return input;            

            }            

            else            

            {                

                return input.Substring(0, length) + "...";            

            }        

        }    

    }


这是一个截断字符串长度的函数。

这样使用

// 先添加命名空间@using MvcMusicStore.HtmlHelpers// 调用@Html.Truncate(item.Title, 25)

效果

image

原文地址:http://ninofocus.com/2011/03/23/add-custom-mothod-to-helper/


Last modified by tommyhu on2012/04/03 09:53
Add a comment
Emots
emotemotemotemotemotemotemotemotemotemotemotemotemotemotemotemot
Enable HTML
Enable UBB
Enable Emots
Hidden
Remember

Nickname : 

Password(可不填) :  Optional

Site URI (可不填) : 

Email (可不填) :  [Register]