我一直在浏览网页,试图找到一个很好的示例/教程,详细说明如何创建和使用我自己的自定义HTML助手为我的MVC 3 Razor应用程序,我发现这是一个如下向MVC项目添加自定义HTML助手
Adding your own HtmlHelper in ASP.NET MVC 3
我创建了一个类(修剪下来一点)为使
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"));
}
}
}
到目前为止好,
在我的控制器我还添加了参考现在
using System.Web.Mvc.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?)
可能有人请告诉我是什么即时在这里做错了。
您是否有一个名为DisplayImageFor的HTML帮助器? –
@UfukHacıoğulları对不起,我添加了不正确的错误陈述,我更新了上述内容。 –