嗨,我已經得到了擴展方法在我PagingHelpers類:MVC擴展方法錯誤
namespace SportsStore.WebUI.HtmlHelpers
{
public static class PagingHelpers
{
public static MvcHtmlString PageLinks(this HtmlHelper html,
PagingInfo pagingInfo,
Func<int, string> pageUrl)
{
StringBuilder result = 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");
result.Append(tag.ToString());
}
return MvcHtmlString.Create(result.ToString());
}
}
}
在這裏,我呼籲在List.cshtml擴展方法:
@Html.PageLinks(Model.PagingInfo, x => Url.Action("List", new {page = x}))
我得到這個錯誤:
'System.Web.Mvc.HtmlHelper' does not contain a definition for 'PageLinks' and no extension method 'PageLinks' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?)
我在視圖文件夾中的web.config中添加了命名空間:
<pages>
<namespaces>
<add namespace="System.Web.Helpers" />
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
<add namespace="System.Web.WebPages"/>
<add namespace="SportsStore.WebUI.HtmlHelpers"/>**
</namespaces>
</pages>
請幫助我,我不知道我怎麼能解決這個問題
它的工作原理。謝謝! – mrtn 2011-12-26 21:44:35
沒問題,你的命名空間方法應該也能正常工作,所以嘗試擊落服務器重新生成你的解決方案並再次運行,也許它不會使用@using語句 – 2011-12-26 21:46:22
將命名空間添加到views/web.config應該足夠。我懷疑該項目在嘗試使用html助手擴展之前未被重新編譯。 – santiagoIT 2011-12-26 21:55:24