C# 向MVC项目添加自定义HTML帮助程序

C# 向MVC项目添加自定义HTML帮助程序,c#,asp.net-mvc,asp.net-mvc-3,razor,html-helper,C#,Asp.net Mvc,Asp.net Mvc 3,Razor,Html Helper,我一直在浏览网页,试图找到一个很好的示例/教程,详细说明如何为我的MVC 3 Razor应用程序创建和使用我自己的自定义HTML助手。我找到了这个示例/教程,如下所示 我已经创建了一个类(稍微精简了一点) 现在在我看来,我有以下几点 @Html.StateDropDownList(x => x.State) 但是我得到了以下错误 System.web.mvc.htmlhelper<system.collections.generic.list<Profile.Profile

我一直在浏览网页,试图找到一个很好的示例/教程,详细说明如何为我的MVC 3 Razor应用程序创建和使用我自己的自定义HTML助手。我找到了这个示例/教程,如下所示

我已经创建了一个类(稍微精简了一点)

现在在我看来,我有以下几点

@Html.StateDropDownList(x => x.State)
但是我得到了以下错误

System.web.mvc.htmlhelper<system.collections.generic.list<Profile.ProfileImages>> does     not contain a definition for StateDropDownList and no extension method     StateDropDownList acception a first argument of type     system.web.mvc.htmlhelper<System.Collections.Generic.List<Profile.ProfileImages>> could be      found(Are you missing a using directive of an assembly reference?)
System.web.mvc.htmlhelper不包含StateDropDownList的定义,并且没有扩展方法StateDropDownList接受。可以找到System.web.mvc.htmlhelper类型的第一个参数(是否缺少程序集引用的using指令?)

谁能告诉我我做错了什么。

您应该在视图中包含名称空间:

@using MyWebApp
也可以从web.config为所有视图导入此命名空间


我认为更简单的选择是执行以下操作: 1.像往常一样创建一个视图,像往常一样使用代码和html的混合方式将助手放在其中。 2.将视图移动到App_Code文件夹。 3.获取所有视图中的帮助(请注意_MyHelpers是App_Code文件夹中视图的名称):

这是App_Code文件夹中的一个视图示例,可在上述任何视图上访问:

    @helper JQMRadioList(string Legend, string RadioListName, List<Fizz.DomainClasses.GenericClasses.GenericDropDownOption> options, bool Horizontal = false, string CurrentSelection = "")
    {
        <fieldset data-role="controlgroup" data-mini="true" data-theme="b" @((Horizontal) ? " data-type=\"horizontal\"" : "")>
            <legend>@Legend:</legend>
            @foreach (var li in options)
            {
                @JQMRadioListItem(RadioListName, li.TheValue, li.Text, CurrentSelection)
            }
        </fieldset>
    }
@helper JQMRadioList(字符串图例、字符串RadioListName、列表选项、bool Horizontal=false、字符串CurrentSelection=“”)
{
@图例:
@foreach(期权中的var li)
{
@JQMRadioistItem(RadioistName、li.TheValue、li.Text、CurrentSelection)
}
}

您是否有名为DisplayImageFor的HTML帮助程序?@UfukHacıoğulları抱歉,我添加了错误的错误声明,我已更新了上述内容。感谢您的帮助。不确定是谁否决了此操作,但我可以问一下原因吗?此答案显示了如何在视图中创建帮助程序。它是基于代码的视图的替代方案,没有理由投反对票。
@using MyWebApp
@_MyHelpers.JQMRadioTrueFalse("Voice Mail on your Home Phone?", "HomePhoneHasAnswerPhone", Model.TrueFalse, t.HomePhoneHasAnswerPhone)
    @helper JQMRadioList(string Legend, string RadioListName, List<Fizz.DomainClasses.GenericClasses.GenericDropDownOption> options, bool Horizontal = false, string CurrentSelection = "")
    {
        <fieldset data-role="controlgroup" data-mini="true" data-theme="b" @((Horizontal) ? " data-type=\"horizontal\"" : "")>
            <legend>@Legend:</legend>
            @foreach (var li in options)
            {
                @JQMRadioListItem(RadioListName, li.TheValue, li.Text, CurrentSelection)
            }
        </fieldset>
    }