L'ajout d'une assistance HTML personnalisée de Projet MVC

J'ai été la navigation sur le web en essayant de trouver un bon exemple/tutoriel expliquant en détail comment je peux créer et utiliser mon propre HTML Helpers pour mon MVC 3 Rasoir application, j'ai trouvé celui-ci qui est comme suit

L'ajout de votre propre HtmlHelper dans ASP.NET MVC 3

J'ai créé une classe (réduit un peu) comme

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,

À l'intérieur de mon contrôleur j'ai aussi ajouté la référence

using System.Web.Mvc.Html;

Maintenant à l'intérieur de mon point de vue, j'ai le texte suivant

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

Mais j'obtiens l'erreur suivante

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

Quelqu'un pourrait s'il vous plaît dites-moi ce que im faire de mal ici.

  • Avez-vous un helper HTML appelé DisplayImageFor?
  • Désolé, j'ai ajouté les incorrecte erreur de déclaration, j'ai mis à jour ci-dessus.
InformationsquelleAutor Code Ratchet | 2013-09-21