El asistente Html personalizado no funciona
Frecuentes
Visto 2,330 veces
0
Estoy tratando de aprender MVC3 de Pro ASP.NET MVC3 Framework. Pero estoy atascado en un lugar donde agregamos Html Helper personalizado.
Hice todo lo mencionado en el libro, pero no puedo agregar el asistente Html personalizado.
¿Alguien puede ayudar por favor?
Muchas Gracias
List.cshtml
@model SportsStore.WebUI.Models.ProductListViewModel
@{
ViewBag.Titke = "Product";
}
<!DOCTYPE html>
<html>
<head>
<title>List</title>
</head>
<body>
<div>
@foreach (var p in Model.Products)
{
<div class="item">
@p.Name
@p.Description
<h4>@p.Price.ToString("c")</h4>
</div>
}
<div class="Pager">
@Html.PageLinks(Model.pagingInfo, x => Url.Action("List", new {page = x}))
</div>
</div>
</body>
</html>
PagingHelper.Cs
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SportsStore.WebUI.Models;
using System.Text;
namespace SportsStore.WebUI.HtmlHelpers
{
public static class PagingHelper
{
public static MvcHtmlString PageLinks(HtmlHelper helper, PagingInfo pagingInfo, Func<int, string> pageUrl)
{
StringBuilder linkString = new StringBuilder();
for (int i = 1; i <= pagingInfo.TotalPages; i++)
{
TagBuilder tag = new TagBuilder("a");
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagingInfo.CurrentPage)
{
tag.AddCssClass("selected");
}
linkString.Append(tag.ToString());
}
return MvcHtmlString.Create(linkString.ToString());
}
}
}
Web.Config
<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<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.Routing" />
<add namespace="SportsStore.WebUI.HtmlHelpers" />
</namespaces>
</pages>
</system.web.webPages.razor>
1 Respuestas
3
No creaste un método de extensión.
Para hacer un método de extensión, necesita decorar el primer parámetro con el this
palabra clave.
Respondido el 31 de diciembre de 12 a las 15:12
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas asp.net-mvc-3 razor html-helper or haz tu propia pregunta.
Error: System.Web.Mvc.HtmlHelper ' no contiene una definición para 'PageLinks' - Vin05
@ Vin05: como SLaks indicó en su respuesta, la firma de la función debe ser
public static MvcHtmlString PageLinks(this HtmlHelper helper ...
- Joel Etherton@Joel Etherton: sí, funciona cuando agrego esto (palabra clave). Gracias - Vin05