自定义MVC帮助程序StateDropDownListFor无法正确使用intellisense

时间:2013-12-19 16:33:56

标签: c# asp.net-mvc asp.net-mvc-4 intellisense html-helper

我有一个自定义的Html Helper工作正常,但intellisense的行为与普通DropDownListFor的行为不同。我的帮助器有两个参数 - 一个表达式,用于标识应用帮助程序的模型的属性;和htmlAttributes的对象。对于正常的DropDownListFor,每当我开始输入htmlAttributes时,它就会知道我尝试输入classidwidth。但是,使用我的自定义HtmlHelper,它不知道class或任何html属性是什么。它没有抱怨,它建立/工作得很好。我只是想知道为什么intellisense没有为我的自定义HtmlHelper获取任何html属性。我已经尝试将命名空间添加到我的视图以及我的web.config,但这些也没有帮助。这是我的帮手:

public static MvcHtmlString StateDropDownListFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
{
   Dictionary<string, string> stateList = new Dictionary<string, string>()
  {
     {"", "Select a State"},
     {"AL", " Alabama"},
     {"AK", " Alaska"},
     {"AZ", " Arizona"},
     {"AR", " Arkansas"},
     {"CA", " California"},
     {"CO", " Colorado"},
     {"CT", " Connecticut"},
     {"DE", " Delaware"},
     {"DC", " District of Columbia"},
     {"FL", " Florida"},
     {"GA", " Georgia"},
     {"HI", " Hawaii"},
     {"ID", " Idaho"},
     {"IL", " Illinois"},
     {"IN", " Indiana"},
     {"IA", " Iowa"},
     {"KS", " Kansas"},
     {"KY", " Kentucky"},
     {"LA", " Louisiana"},
     {"ME", " Maine"},
     {"MD", " Maryland"},
     {"MA", " Massachusetts"},
     {"MI", " Michigan"},
     {"MN", " Minnesota"},
     {"MS", " Mississippi"},
     {"MO", " Missouri"},
     {"MT", " Montana"},
     {"NE", " Nebraska"},
     {"NV", " Nevada"},
     {"NH", " New Hampshire"},
     {"NJ", " New Jersey"},
     {"NM", " New Mexico"},
     {"NY", " New York"},
     {"NC", " North Carolina"},
     {"ND", " North Dakota"},
     {"OH", " Ohio"},
     {"OK", " Oklahoma"},
     {"OR", " Oregon"},
     {"PA", " Pennsylvania"},
     {"PR", " Puerto Rico"},
     {"RI", " Rhode Island"},
     {"SC", " South Carolina"},
     {"SD", " South Dakota"},
     {"TN", " Tennessee"},
     {"TX", " Texas"},
     {"UT", " Utah"},
     {"VT", " Vermont"},
     {"VA", " Virginia"},
     {"WA", " Washington"},
     {"WV", " West Virginia"},
     {"WI", " Wisconsin"},
     {"WY", " Wyoming"}
     //{"AS", " American Samoa"},
     //{"FM", " Federated States of Micronesia"},
     //{"MH", " Marshall Islands"},
     //{"MP", " Northern Mariana Islands"},
     //{"PW", " Palau"},
     //{"VI", " Virgin Islands"},
     //{"GU", " Guam"}
  };
  return html.DropDownListFor(expression, new SelectList(stateList, "key", "value"), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
}

web.config:

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization"/>
        <add namespace="System.Web.Routing" />
        <add namespace="SEGDatabase.Helpers"/>
      </namespaces>
    </pages>
  </system.web.webPages.razor>

1 个答案:

答案 0 :(得分:0)

首先,在web.config文件(Views / Web.Config)中添加程序集,如此

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <!-- THIS!! -->
        <add namespace="MyProject.My.Name.Space.For.Html.Extensions" /> 
      </namespaces>
    </pages>
 </system.web.webPages.razor>

其次,确保状态列表是扩展类中的静态对象(否则它是非常低效的)

        public static MvcHtmlString StateDropDownListFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object htmlAttributes)
        {

            return html.DropDownListFor(expression, new SelectList(_stateList, "key", "value"), HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes));
        }

        private static readonly Dictionary<string,string> _stateList = new Dictionary<string, string>()
            {
                 {"", "Select a State"},
                 {"AL", " Alabama"},
                 {"AK", " Alaska"},
                 {"AZ", " Arizona"},
                 {"AR", " Arkansas"},
                 {"CA", " California"},
                 {"CO", " Colorado"},
                 {"CT", " Connecticut"},
                 {"DE", " Delaware"},
                 {"DC", " District of Columbia"},
                 {"FL", " Florida"},
                 {"GA", " Georgia"},
                 {"HI", " Hawaii"},
                 {"ID", " Idaho"},
                 {"IL", " Illinois"},
                 {"IN", " Indiana"},
                 {"IA", " Iowa"},
                 {"KS", " Kansas"},
                 {"KY", " Kentucky"},
                 {"LA", " Louisiana"},
                 {"ME", " Maine"},
                 {"MD", " Maryland"},
                 {"MA", " Massachusetts"},
                 {"MI", " Michigan"},
                 {"MN", " Minnesota"},
                 {"MS", " Mississippi"},
                 {"MO", " Missouri"},
                 {"MT", " Montana"},
                 {"NE", " Nebraska"},
                 {"NV", " Nevada"},
                 {"NH", " New Hampshire"},
                 {"NJ", " New Jersey"},
                 {"NM", " New Mexico"},
                 {"NY", " New York"},
                 {"NC", " North Carolina"},
                 {"ND", " North Dakota"},
                 {"OH", " Ohio"},
                 {"OK", " Oklahoma"},
                 {"OR", " Oregon"},
                 {"PA", " Pennsylvania"},
                 {"PR", " Puerto Rico"},
                 {"RI", " Rhode Island"},
                 {"SC", " South Carolina"},
                 {"SD", " South Dakota"},
                 {"TN", " Tennessee"},
                 {"TX", " Texas"},
                 {"UT", " Utah"},
                 {"VT", " Vermont"},
                 {"VA", " Virginia"},
                 {"WA", " Washington"},
                 {"WV", " West Virginia"},
                 {"WI", " Wisconsin"},
                 {"WY", " Wyoming"}
        };

一切都应该好。