开发者

How can I have multiple HtmlHelperExtensions in MVC3

I created one file and added a HtmlHelperExtensions class.

  public static class HtmlHelperExtensions {

        private const string Nbsp = " ";
        private const string SelAttribute = " selected='selected'";

        public static MvcHtmlString NbspIfEmpty(this HtmlHelper helper, string value)
        {
            var str = string.IsNullOrEmpty(value) ? Nbsp : value;
            return new MvcHtmlString(str);
        }

        etc...

Now I would like to add more files with more HtmlHelperExtensions. However when I do this开发者_如何转开发 I get an error saying:

Duplicate definition: HtmlHelperExtensions

Is it possible for me to have more than one of these classes?


Just name the class something different. You're not allowed duplicate type names under one namespace.

Here's a good tutorial on creating custom Html helpers: http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs.


You could name the class differently as stated by @Andrew Whitaker or you could use the partial keyword.

public static partial class HtmlHelperExtensions 
{
    // helpers ...
}

public static partial class HtmlHelperExtensions 
{
    // other helpers ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜