我的 View 中有一个 Html.GlobalizedPageLinks 扩展方法,但出现一条红线,说明我的 View 模型不包含该方法并且我有一些无效参数?
这里是:
<div class="actions-left">
<%= Html.GlobalisedPageLinks(Amico.Web.Mvc.Extensions.Enums.PageLinksFormat.Empty, Model.CurrentPage, Model.PageSize, Model.Total, x => Url.Action("Index", "Scorm", new { area = "Admin", page = x }))%>
</div>
扩展方法:
public static string GlobalisedPageLinks(this HtmlHelper html, Amico.Web.Mvc.Extensions.Enums.PageLinksFormat format, int currentPage, int pageSize, int totalResults, Func<int, string> pageUrl)
{
int totalPages = Math.Max(Convert.ToInt32(Math.Ceiling((double)totalResults / pageSize)), 1);
int startresult = ((Math.Max(1, currentPage) - 1) * pageSize) + 1;
int endresult = Math.Min(startresult + (pageSize - 1), totalResults);
string pagesText = html.Resource(Resources.Global.PageLinks.PageLinksFormatPages, currentPage, totalPages);
string resultsText = html.Resource(Resources.Global.PageLinks.PageLinksFormatResults, startresult, endresult, totalResults);
string firstText = html.Resource(Resources.Global.PageLinks.First);
string previousText = html.Resource(Resources.Global.PageLinks.Previous);
string nextText = html.Resource(Resources.Global.PageLinks.Next);
string lastText = html.Resource(Resources.Global.PageLinks.Last);
return "<span class='page-links'>" + html.PageLinks(format, currentPage, pageSize, totalResults, pageUrl,
pagesText, resultsText, firstText, previousText, nextText, lastText) + "</span>";
}
我错过了什么? 谢谢
最佳答案
您需要在 View 中向包含扩展方法的类添加一条 using 语句:
@using Amico.Web.Mvc.Extensions.YourExtensionClass
如果您需要在多个 View 中访问此扩展类,您还可以将其命名空间添加为 View 文件夹内的 web.Config 中的已知命名空间(此示例适用于 MVC3,MVC4 的主机将有所不同) :
<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="Amico.Web.Mvc.Extensions" />
</namespaces>
</pages>
</system.web.webPages.razor>
关于asp.net-mvc-3 - View 模型无法识别扩展方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15289445/