asp.net-mvc System.Web.mvc.HtmlHelper 不包含 EnumDropDownListFor 的定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10761484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
System.Web.mvc.HtmlHelper does not contain a definition for EnumDropDownListFor
提问by user1382770
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.aspxbut I run into the error "System.Web.Mvc.HtmlHelper does not contain a definition for EnumDropDownListFor".
我一直在关注本教程http://blogs.msdn.com/b/stuartleeks/archive/2010/05/21/asp-net-mvc-creating-a-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)
回答by Darin Dimitrov
You forgot to bring the extension method in scope inside the view. This EnumDropDownListFormethod 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:
您需要在要使用此帮助程序的视图中添加此命名空间:
@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.configfile:
为了避免将此 using 子句添加到所有 Razor 视图中,您还可以将其添加到文件的<namespaces>部分~/Views/web.config:
<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>
回答by user1477388
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 HtmlHelperso if you add the wrong one, you will get this error. Replacing it with the correct namespace will fix it.
两者都有定义,HtmlHelper因此如果添加错误的定义,则会出现此错误。用正确的命名空间替换它会修复它。
回答by Gavin
Change your namespace to
将您的命名空间更改为
namespace System.Web.Mvc
{
public static class HtmlExtensions
{
...
回答by zomf
In my case I had my extensions namespace referenced, but was missing a using statement for System.Web.Mvc.Html.
就我而言,我引用了我的扩展命名空间,但缺少 System.Web.Mvc.Html 的 using 语句。
(The DropDownList methods are defined in System.Web.Mvc.Html.SelectExtensions)
(DropDownList 方法在 System.Web.Mvc.Html.SelectExtensions 中定义)
回答by ArhiChief
Maybe the problem is that your HtmlHelperdoesn't contain defination for DropDownListbecause of HtmlHelperdefined in several namespaces: System.Web.Mvcand System.Web.WebPages.Html. The System.Web.WebPages.Htmlnamespace contains HtmlHelper.DropDownList.
也许问题在于您HtmlHelper不包含定义 forDropDownList因为HtmlHelper在多个命名空间中定义:System.Web.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 命名空间发布到 Views Web.config 中,这样 Razor 视图引擎会找到它。
回答by brsfan
Pay particular attention in Darin's answer that you will be adding your namespace to the web.config in the Viewsfolder. Notthe web.configfile 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.configfile.
请特别注意 Darin 的回答,您将把您的命名空间添加到Views文件夹中的 web.config 中。没有了web.config你的根Web文件夹的文件。一开始我没有注意到它,有一段时间不明白为什么在将我的命名空间添加到根web.config文件后它不起作用。
I think this also explains why it works for those who change their namespace to System.Web.Mvcin their HtmlExtensions class. System.Web.Mvcis already included in the namespaces in the ~/Views/web.configfile.
我认为这也解释了为什么它适用于那些将命名空间更改为System.Web.MvcHtmlExtensions 类的人。System.Web.Mvc已包含在~/Views/web.config文件的命名空间中。
回答by Barry MSIH
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.
如前所述,正确的命名空间需要位于 views 文件夹下的 web.config 文件中。默认命名空间自动包含在 views 文件夹下的 web.config 文件中。
If your default namespace is
如果您的默认命名空间是
namespace AppName.SomeNamespace
The web.config file already contains the entry:
web.config 文件已包含以下条目:
<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 项目中创建一个新文件夹时,命名空间会得到扩展。如果您像我一样创建了一个名为 Helpers 的文件夹,那么这些方法的命名空间将是:
namespace AppName.SomeNamespace.Helpers
The extended namespace is not in the web in the web.config file under the views folder.
扩展命名空间不在 web.config 文件中的 views 文件夹下。
You have two options:
您有两个选择:
Change the namespace in the file with the html helper methods to the default namespace by removing the ".Helpers".
Add the extended namespace to the web config file
<add namespace="AppName.SomeNamespace.Helpers">
通过删除“.Helpers”,使用 html helper 方法将文件中的命名空间更改为默认命名空间。
将扩展命名空间添加到 web 配置文件
<add namespace="AppName.SomeNamespace.Helpers">
回答by Drew
We use VB and were having the same issue. The only thing that worked for us was, in the _PartialView.vbhtml page:
我们使用 VB 并且遇到了同样的问题。唯一对我们有用的是在 _PartialView.vbhtml 页面中:
@* Import the project's root namespace *@
@Imports MyRootNamespace
@* Import the Module that contains the HTML Extension Functions *@
@Imports MyHtmlHelperExtensions
@* To use the extension, We have to call the extension function directly
Passing the Html (HTML Helper Object) as the first parameter. *@
MyExtensionFunctionName(Html, "Hello World")
For reference, our HTML Extensions file looks like this:
作为参考,我们的 HTML 扩展文件如下所示:
Imports System.Web.WebPages.Html
Public Module MyHtmlHelperExtensions
'== Example
<Extension()>
Public Function MyExtensionFunctionName(ByVal html As HtmlHelper, ByVal textToDisplay As String) As IHtmlString
html.Raw(textToDisplay)
End Function
End Module

