System.Web.mvc。HtmlHelper不包含EnumDropDownListFor的定义

时间:2021-07-20 13:28:06

I have been following this tutorial http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-dropdownlist-helper-for-enums.aspx but I run into the error "System.Web.Mvc.HtmlHelper does not contain a definition for EnumDropDownListFor".

我一直在学习本教程http://blogs.msdn.com/b/stuartleeks/archive/20105/21/asp-net - creating-dropdownlist -helper-for enums.aspx,但是我遇到了错误“System.Web.Mvc”。HtmlHelper不包含EnumDropDownListFor的定义”。

Model:

模型:

public enum Codes
{
    IBC2012,
    IBC2009,
    IBC2006,
    FL2010,
    CBC2007
}

public class Code
{
    public int ID { get; set; }
    public int Active { get; set; }
    public string Description { get; set; }
    public string Edition { get; set; }
    public Codes Code { get; set; }
}

Controller:

控制器:

public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
    {
        ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        IEnumerable<TEnum> values = Enum.GetValues(typeof(TEnum)).Cast<TEnum>();

        IEnumerable<SelectListItem> items =
            values.Select(value => new SelectListItem
            {
                Text = value.ToString(),
                Value = value.ToString(),
                Selected = value.Equals(metadata.Model)
            });

        return htmlHelper.DropDownListFor(
            expression,
            items
            );
    }

HTML Helper:

HTML帮助器:

@Html.EnumDropDownListFor(model => model.Code.Codes)

Any help is appreciated.

任何帮助都是感激。

7 个解决方案

#1


21  

You forgot to bring the extension method in scope inside the view. This EnumDropDownListFor method is defined in some static class inside a namespace, right?

您忘记将扩展方法放在视图的范围内。这个EnumDropDownListFor方法是在名称空间中的某个静态类中定义的,对吗?

namespace AppName.SomeNamespace
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
        {
            ...
        }
    }
}

You need to add this namespace inside the view in which you want to use this helper:

您需要在想要使用这个helper的视图中添加这个名称空间:

@using AppName.SomeNamespace
@model MyViewModel
...
@Html.EnumDropDownListFor(model => model.Code.Codes)

And to avoid adding this using clause to all your Razor views you could also add it to the <namespaces> section of your ~/Views/web.config file:

为了避免将这个using子句添加到所有的Razor视图中,还可以将它添加到~/ views /web的 部分。配置文件:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.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" />
        <add namespace="AppName.SomeNamespace" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

#2


4  

As @ArhiChief hinted at in their answer, my problem was:

正如@ArhiChief在他们的回答中所暗示的,我的问题是:

using System.Web.WebPages.Html;

Instead of:

而不是:

using System.Web.Mvc;

Both of which have definitions for HtmlHelper so if you add the wrong one, you will get this error. Replacing it with the correct namespace will fix it.

两者都有HtmlHelper的定义,所以如果你添加了错误的一个,你会得到这个错误。用正确的名称空间替换它将修复它。

#3


3  

Change your namespace to

改变你的名称空间

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
...

#4


2  

In my case I had my extensions namespace referenced, but was missing a using statement for System.Web.Mvc.Html.

在我的例子中,我引用了扩展名称空间,但是缺少System.Web.Mvc.Html的使用语句。

(The DropDownList methods are defined in System.Web.Mvc.Html.SelectExtensions)

(DropDownList方法在system . web . mvc.html . selectextense)

#5


2  

Maybe the problem is that your HtmlHelper doesn't contain defination for DropDownList because of HtmlHelper defined in several namespaces: System.Web.Mvc and System.Web.WebPages.Html. The System.Web.WebPages.Html namespace contains HtmlHelper.DropDownList.

可能问题是,由于在几个名称空间System.Web中定义了html助手,所以html助手不包含下拉列表的定义。Mvc和System.Web.WebPages.Html。System.Web.WebPages。Html命名空间包含HtmlHelper.DropDownList。

Also, don't forget to post your html helper namespace into Views Web.config, so, Razor view engine will find it.

另外,不要忘记将html helper名称空间放到视图Web中。配置,剃刀视图引擎会找到它。

#6


1  

Pay particular attention in Darin's answer that you will be adding your namespace to the web.config in the Views folder. Not the web.config file in your root web folder. I didn't notice it at first, and for a while was baffled as to why it wouldn't work after adding my namespace to the root web.config file.

请特别注意达林的回答,您将向web添加名称空间。在视图文件夹中配置。没有网络。根web文件夹中的配置文件。一开始我没有注意到它,有一段时间我很困惑,为什么在将命名空间添加到根web之后它就不能工作。配置文件。

I think this also explains why it works for those who change their namespace to System.Web.Mvc in their HtmlExtensions class. System.Web.Mvc is already included in the namespaces in the ~/Views/web.config file.

我认为这也解释了为什么它适用于那些将名称空间更改为System.Web的人。在他们的htmlextense类中的Mvc。包含。Mvc已经包含在~/Views/web中的名称空间中。配置文件。

#7


0  

As previously stated, the correct namespace needs to be in the web.config file under the views folder. The default namespace is automatically included in the web.config file under the views folder.

如前所述,正确的名称空间需要位于web中。视图文件夹下的配置文件。默认的名称空间将自动包含在web中。视图文件夹下的配置文件。

If your default namespace is

如果您的默认名称空间是

 namespace AppName.SomeNamespace

The web.config file already contains the entry:

网络。配置文件已经包含了条目:

 <add namespace="AppName.SomeNamespace" />

What has not been mentioned before is when you create a new folder in your MVC project the namespace gets extended. If you created a folder called Helpers, like I did then your namespace for these methods will be:

以前没有提到的是,在MVC项目中创建新文件夹时,名称空间会得到扩展。如果您创建了一个名为helper的文件夹,如我所做的,那么这些方法的名称空间将是:

 namespace AppName.SomeNamespace.Helpers

The extended namespace is not in the web in the web.config file under the views folder.

扩展的名称空间不在web中。视图文件夹下的配置文件。

You have two options:

你有两个选择:

  1. Change the namespace in the file with the html helper methods to the default namespace by removing the ".Helpers".

    通过删除“. helper”,使用html helper方法将文件中的名称空间更改为默认名称空间。

  2. Add the extended namespace to the web config file

    向web配置文件添加扩展的名称空间

    <add namespace="AppName.SomeNamespace.Helpers">
    

#1


21  

You forgot to bring the extension method in scope inside the view. This EnumDropDownListFor method is defined in some static class inside a namespace, right?

您忘记将扩展方法放在视图的范围内。这个EnumDropDownListFor方法是在名称空间中的某个静态类中定义的,对吗?

namespace AppName.SomeNamespace
{
    public static class HtmlExtensions
    {
        public static MvcHtmlString EnumDropDownListFor<TModel, TEnum>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TEnum>> expression)
        {
            ...
        }
    }
}

You need to add this namespace inside the view in which you want to use this helper:

您需要在想要使用这个helper的视图中添加这个名称空间:

@using AppName.SomeNamespace
@model MyViewModel
...
@Html.EnumDropDownListFor(model => model.Code.Codes)

And to avoid adding this using clause to all your Razor views you could also add it to the <namespaces> section of your ~/Views/web.config file:

为了避免将这个using子句添加到所有的Razor视图中,还可以将它添加到~/ views /web的 部分。配置文件:

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.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" />
        <add namespace="AppName.SomeNamespace" />
      </namespaces>
    </pages>
</system.web.webPages.razor>

#2


4  

As @ArhiChief hinted at in their answer, my problem was:

正如@ArhiChief在他们的回答中所暗示的,我的问题是:

using System.Web.WebPages.Html;

Instead of:

而不是:

using System.Web.Mvc;

Both of which have definitions for HtmlHelper so if you add the wrong one, you will get this error. Replacing it with the correct namespace will fix it.

两者都有HtmlHelper的定义,所以如果你添加了错误的一个,你会得到这个错误。用正确的名称空间替换它将修复它。

#3


3  

Change your namespace to

改变你的名称空间

namespace System.Web.Mvc
{
    public static class HtmlExtensions
    {
...

#4


2  

In my case I had my extensions namespace referenced, but was missing a using statement for System.Web.Mvc.Html.

在我的例子中,我引用了扩展名称空间,但是缺少System.Web.Mvc.Html的使用语句。

(The DropDownList methods are defined in System.Web.Mvc.Html.SelectExtensions)

(DropDownList方法在system . web . mvc.html . selectextense)

#5


2  

Maybe the problem is that your HtmlHelper doesn't contain defination for DropDownList because of HtmlHelper defined in several namespaces: System.Web.Mvc and System.Web.WebPages.Html. The System.Web.WebPages.Html namespace contains HtmlHelper.DropDownList.

可能问题是,由于在几个名称空间System.Web中定义了html助手,所以html助手不包含下拉列表的定义。Mvc和System.Web.WebPages.Html。System.Web.WebPages。Html命名空间包含HtmlHelper.DropDownList。

Also, don't forget to post your html helper namespace into Views Web.config, so, Razor view engine will find it.

另外,不要忘记将html helper名称空间放到视图Web中。配置,剃刀视图引擎会找到它。

#6


1  

Pay particular attention in Darin's answer that you will be adding your namespace to the web.config in the Views folder. Not the web.config file in your root web folder. I didn't notice it at first, and for a while was baffled as to why it wouldn't work after adding my namespace to the root web.config file.

请特别注意达林的回答,您将向web添加名称空间。在视图文件夹中配置。没有网络。根web文件夹中的配置文件。一开始我没有注意到它,有一段时间我很困惑,为什么在将命名空间添加到根web之后它就不能工作。配置文件。

I think this also explains why it works for those who change their namespace to System.Web.Mvc in their HtmlExtensions class. System.Web.Mvc is already included in the namespaces in the ~/Views/web.config file.

我认为这也解释了为什么它适用于那些将名称空间更改为System.Web的人。在他们的htmlextense类中的Mvc。包含。Mvc已经包含在~/Views/web中的名称空间中。配置文件。

#7


0  

As previously stated, the correct namespace needs to be in the web.config file under the views folder. The default namespace is automatically included in the web.config file under the views folder.

如前所述,正确的名称空间需要位于web中。视图文件夹下的配置文件。默认的名称空间将自动包含在web中。视图文件夹下的配置文件。

If your default namespace is

如果您的默认名称空间是

 namespace AppName.SomeNamespace

The web.config file already contains the entry:

网络。配置文件已经包含了条目:

 <add namespace="AppName.SomeNamespace" />

What has not been mentioned before is when you create a new folder in your MVC project the namespace gets extended. If you created a folder called Helpers, like I did then your namespace for these methods will be:

以前没有提到的是,在MVC项目中创建新文件夹时,名称空间会得到扩展。如果您创建了一个名为helper的文件夹,如我所做的,那么这些方法的名称空间将是:

 namespace AppName.SomeNamespace.Helpers

The extended namespace is not in the web in the web.config file under the views folder.

扩展的名称空间不在web中。视图文件夹下的配置文件。

You have two options:

你有两个选择:

  1. Change the namespace in the file with the html helper methods to the default namespace by removing the ".Helpers".

    通过删除“. helper”,使用html helper方法将文件中的名称空间更改为默认名称空间。

  2. Add the extended namespace to the web config file

    向web配置文件添加扩展的名称空间

    <add namespace="AppName.SomeNamespace.Helpers">