自定义 MVC 帮助程序 StateDropDownListFor 无法正确使用智能感知



我有一个自定义的 Html 助手,它工作得很好,但智能感知的行为不像普通DropDownListFor那样。 我的帮助程序有两个参数 - 一个表达式,用于标识应用帮助程序的模型的属性;和htmlAttributes的对象. 使用正常的DropDownListFor,每当我开始键入htmlAttributes时,它就会知道我正在尝试键入classidwidth。 但是,使用我的自定义 HtmlHelper,它不知道class或任何 html 属性是什么。 它不会抱怨,并且可以很好地构建/工作。 我只是想知道为什么智能感知没有为我的自定义 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));
}

网络配置:

  <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>

首先,将程序集添加到您的 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"}
        };

一切都应该好起来的。

相关内容

  • 没有找到相关文章

最新更新