Witam, próbuję rozszerzyć klasę HTMLHelper o metodę, która weźmie w tag tekst i wyświetli go wielkimi literami. Taki przykład znalazłem w magazynie Programista i nawet gdy kopiuję, to nie działa.
Stworzyłem nową klasę w projekcie:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcApplication1
{
using System.Web.Mvc;
/// <summary>
/// Klasa naszego helpera
/// </summary>
public static class CustomHelpers
{
/// <summary>
/// Nasz własny helper
/// /// </summary>
/// <param name="helper">obiekt rozszerzanej klasy</param>
/// <param name="text">parametr wejściowy</param>
/// <returns>wynik metody</returns>
public static MvcHtmlString ToUpperHelper(this HtmlHelper helper, string text)
{
return MvcHtmlString.Create(string.Format("<label>{0}</label>", text.ToUpper()));
}
}
}
Tym razem DOSŁOWNIE skopiowałem. Oczywiście tutaj żadnych błędów kompilator nie widzi.
Jednak gdy chcę tego użyć w widoku, to po wpisaniu:
@Html.
VS nie podpowiada metody ToUpperHelper. Wpisując ją żywcem
@Html.ToUpperHelper(item.Title)
Komunikat o błędzie kompilatora: CS1061: Element „System.Web.Mvc.HtmlHelper<System.Collections.Generic.IEnumerable<MvcApplication1.Models.Movie>>” nie zawiera definicji „ToUpperHelper”, a nie odnaleziono metody rozszerzającej „ToUpperHelper”, która przyjmuje pierwszy argument typu „System.Web.Mvc.HtmlHelper<System.Collections.Generic.IEnumerable<MvcApplication1.Models.Movie>>” (czy nie brakuje dyrektywy using lub odwołania do zestawu?).
Po wyświetleniu szczegółów mam listę usingów:
iersz 12: namespace ASP {
Wiersz 13: using System;
Wiersz 14: using System.Collections.Generic;
Wiersz 15: using System.IO;
Wiersz 16: using System.Linq;
Wiersz 17: using System.Net;
Wiersz 18: using System.Web;
Wiersz 19: using System.Web.Helpers;
Wiersz 20: using System.Web.Security;
Wiersz 21: using System.Web.UI;
Wiersz 22: using System.Web.WebPages;
Wiersz 23: using System.Web.Mvc;
Wiersz 24: using System.Web.Mvc.Ajax;
Wiersz 25: using System.Web.Mvc.Html;
Wiersz 26: using System.Web.Optimization;
Wiersz 27: using System.Web.Routing;
Wiersz 28: using WebMatrix.Data;
Wiersz 29: using WebMatrix.WebData;
Wiersz 30: using Microsoft.Web.WebPages.OAuth;
Wiersz 31: using DotNetOpenAuth.AspNet;
Siedzę na tym 4-godzinę. Niestety do magazynu nie były dołączone pliki źródłowe. Pomożecie? ;)