添加自定义HTML助手到MVC项目 [英] Adding a custom HTML Helper to MVC Project

查看:124
本文介绍了添加自定义HTML助手到MVC项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在浏览网页试图找到一个很好的例子/教程详细说明如何创建和使用自己的自定义HTML助手为我的MVC 3剃须刀的应用程序,我发现这一个是如下:

添加在ASP.NET MVC 3你自己的HtmlHelper

我创建了一个类(修剪下来了一点),如此

 使用系统;
使用System.Collections.Generic;
使用System.Globalization;
使用System.Linq的;
使用System.Linq.Ex pressions;
使用的System.Web;
使用System.Web.Mvc;
使用System.Web.Mvc.Html;命名空间MyWebApp
{
    公共静态类ExtensionMethods
    {
         公共静态MvcHtmlString StateDropDownListFor<的TModel,TValue>
                        (这与的HtmlHelper LT;的TModel> HTML,
                                        防爆pression<&Func键LT;的TModel,TValue>>前pression)
         {
             字典<字符串,字符串> stateList =新词典<字符串,字符串>()
             {
                {AL,亚拉巴马},
                {AK,阿拉斯加},
                {AZ,亚利桑那},
                {AR,阿肯色}              };
              返回html.DropDownListFor(如pression,
                       新的SelectList(stateList,键,值));
         }     }
}

到目前为止好,

在我的控制器我还添加了引用

 使用System.Web.Mvc.Html;

现在我的观点里,我有以下

  @ Html.StateDropDownList(X => x.State)

不过,我得到以下错误

<$p$p><$c$c>System.web.mvc.htmlhelper<system.collections.generic.list<Profile.ProfileImages>>不包含StateDropDownList的定义,并没有扩展方法StateDropDownList验收规范型system.web.mvc.htmlhelper℃的第一个参数; System.Collections.Generic.List&LT; Profile.ProfileImages&GT;&GT;可以找到(是否缺少using指令程序集引用的?)

可能有人请告诉我做什么我错在这里。


解决方案

您应该包括命名空间在您的视图:

  @using MyWebApp

也可以导入这个命名空间为所有从web.config中的意见。

&LT; system.web.webPages.razor&GT;
  &LT;页面pageBaseType =System.Web.Mvc.WebViewPage&GT;
    &LT;&命名空间GT;
      &LT;添加命名空间=System.Web.Mvc/&GT;
      &LT;添加命名空间=System.Web.Mvc.Ajax/&GT;
      &LT;添加命名空间=System.Web.Mvc.Html/&GT;
      &LT;添加命名空间=System.Web.Optimization/&GT;
      &LT;添加命名空间=System.Web.Routing/&GT;
      &LT;添加命名空间=MyWebApp/&GT;
    &LT; /命名空间&GT;
  &LT; /页&GT;
&LT; /system.web.webPages.razor>

I've been browsing the web trying to find a good example/tutorial detailing how I can create and use my own custom HTML Helpers for my MVC 3 Razor application I found this one which is as follows

Adding your own HtmlHelper in ASP.NET MVC 3

I've created a class (trimmed it down a bit) as so

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace MyWebApp
{
    public static class ExtensionMethods
    {
         public static MvcHtmlString StateDropDownListFor<TModel, TValue>
                        (this HtmlHelper<TModel> html, 
                                        Expression<Func<TModel, TValue>> expression)
         {
             Dictionary<string, string> stateList = new Dictionary<string, string>()
             {
                {"AL"," Alabama"},
                {"AK"," Alaska"},
                {"AZ"," Arizona"},
                {"AR"," Arkansas"}

              };
              return html.DropDownListFor(expression, 
                       new SelectList(stateList, "key", "value"));
         }

     }
}

So far so good,

Inside my controller I also added the reference

using System.Web.Mvc.Html;

Now inside my view i have the following

@Html.StateDropDownList(x => x.State)

But I get the following error

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

Could someone please tell me what im doing wrong here.

解决方案

You should include the namespace in your view:

@using MyWebApp

Or you can import this namespace for all of the views from web.config.

<system.web.webPages.razor>
  <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="MyWebApp" />
    </namespaces>
  </pages>
</system.web.webPages.razor>

这篇关于添加自定义HTML助手到MVC项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆