Asp.net mvc ASP.NET MVC中的嵌套服务器端块?
最近,我在ASP.NETMVC上遇到了一个恼人的情况。简而言之,就是这个故事,Asp.net mvc ASP.NET MVC中的嵌套服务器端块?,asp.net-mvc,server-side-scripting,Asp.net Mvc,Server Side Scripting,最近,我在ASP.NETMVC上遇到了一个恼人的情况。简而言之,就是这个故事, 我应该有一个列出所有产品的视图;现在,因为这些产品太多了,我正在对它们进行分页(非常创新,呵呵!)。该页面包含两个分页箭头-“下一个10个产品”,“和上一个10个产品”。视图将传递一个包含要显示的产品列表的IEnumerable集合。视图还传递两个整数(currentPage、totalPages)作为ViewData项。现在,我需要完成的是检查它是否是第一个页面(ViewData[“CurrentPage”]==0
我应该有一个列出所有产品的视图;现在,因为这些产品太多了,我正在对它们进行分页(非常创新,呵呵!)。该页面包含两个分页箭头-“下一个10个产品”,“和上一个10个产品”。视图将传递一个包含要显示的产品列表的
IEnumerable
集合。视图还传递两个整数(currentPage、totalPages)作为ViewData项。现在,我需要完成的是检查它是否是第一个页面(ViewData[“CurrentPage”]==0),我应该将“前10个页面”链接的css类更改为disabled,因此我提出了如下建议
<a href="/Products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])-1)%>/"
class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 1 ? "bgn disabled" : ""%>">
previous 10 products
</a>
这很好,但还是有问题。尽管该链接已禁用,或者特别是变灰,但它仍然指向有效的URL,因此我尝试根据CurrentPage变量实际更改链接的href属性。下面是代码的样子(准备好面对纯粹的丑陋): <a href="/Products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])-1)%>/"
class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 1 ? "bgn disabled" : ""%>">
previous 10 products
</a>
<a href="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 0 ?
"javascript:void[]" :
"/products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])+1)%>/" %>"
class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 0 ?
"bgn disabled" :
""%>">
previous 10 products
</a>
现在,我对这段代码的问题是: <a href="/Products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])-1)%>/"
class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 1 ? "bgn disabled" : ""%>">
previous 10 products
</a>
有更好的替代方案吗,伙计们?你可以使用
if
语句:
<a href="/Products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])-1)%>/"
class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 1 ? "bgn disabled" : ""%>">
previous 10 products
</a>
<% if (Convert.ToInt32(ViewData["CurrentPage"]) <= 0) { %>
Disabled template goes here...
<% } else { %>
Link template goes here...
<% } %>
您可以尝试我的寻呼机HTML助手:
<a href="/Products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])-1)%>/"
class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 1 ? "bgn disabled" : ""%>">
previous 10 products
</a>
using System;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
namespace System.Web.Mvc
{
public class Pager
{
private ViewContext viewContext;
private readonly int pageSize;
private readonly int currentPage;
private readonly int totalItemCount;
private readonly RouteValueDictionary linkWithoutPageValuesDictionary;
public Pager(ViewContext viewContext, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary)
{
this.viewContext = viewContext;
this.pageSize = pageSize;
this.currentPage = currentPage;
this.totalItemCount = totalItemCount;
this.linkWithoutPageValuesDictionary = valuesDictionary;
}
public string RenderHtml()
{
int pageCount = (int)Math.Ceiling(this.totalItemCount / (double)this.pageSize);
int nrOfPagesToDisplay = 8;
var sb = new StringBuilder();
sb.Append("<ul class=\"pagination\">");
// Previous
if (this.currentPage > 1)
{
sb.Append(string.Format("<li class=\"prev\"><a href=\"{0}\">«</a></li>", Route(this.currentPage - 1)));
}
else
{
sb.Append("<li class=\"prev disabled\"><span>«</span></li>");
}
int start = 1;
int end = pageCount;
if (pageCount > nrOfPagesToDisplay)
{
int middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1;
int below = (this.currentPage - middle);
int above = (this.currentPage + middle);
if (below < 4)
{
above = nrOfPagesToDisplay;
below = 1;
}
else if (above > (pageCount - 4))
{
above = pageCount;
below = (pageCount - nrOfPagesToDisplay);
}
start = below;
end = above;
}
if (start > 3)
{
sb.Append(GeneratePageLink("1", 1));
sb.Append(GeneratePageLink("2", 2));
sb.Append("<li class=\"more\">...</li>");
}
for (int i = start; i <= end; i++)
{
if (i == this.currentPage)
{
sb.Append(string.Format("<li class=\"page selected\"><span>{1}</span></li>", Route(i),i));
}
else
{
sb.Append(GeneratePageLink(i.ToString(), i));
}
}
if (end < (pageCount - 3))
{
sb.Append("<li class=\"more\">...</li>");
sb.Append(GeneratePageLink((pageCount - 1).ToString(), pageCount - 1));
sb.Append(GeneratePageLink(pageCount.ToString(), pageCount));
}
// Next
if (this.currentPage < pageCount)
{
sb.Append(string.Format("<li class=\"next\"><a href=\"{0}\">»</a></li>", Route(this.currentPage + 1)));
}
else
{
sb.Append("<li class=\"next disabled\"><span>»</span></li>");
}
sb.Append("</ul>");
return sb.ToString();
}
private string Route(int pageNumber)
{
var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary);
pageLinkValueDictionary.Add("page", pageNumber);
var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary);
return virtualPathData.VirtualPath;
}
private string GeneratePageLink(string linkText, int pageNumber)
{
var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary);
pageLinkValueDictionary.Add("page", pageNumber);
var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary);
if (virtualPathData != null)
{
string linkFormat = "<li class=\"page\"><a href=\"{0}\">{1}</a></li>";
return String.Format(linkFormat, virtualPathData.VirtualPath, linkText);
}
else
{
return null;
}
}
}
}
最后输出:
<a href="/Products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])-1)%>/"
class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 1 ? "bgn disabled" : ""%>">
previous 10 products
</a>
(来源:)
这里是另一个解决方案。添加
:
<a href="/Products/Page<%=Html.Encode(Convert.ToInt32(ViewData["CurrentPage"])-1)%>/"
class="<%=Convert.ToInt32(ViewData["CurrentPage"]) <= 1 ? "bgn disabled" : ""%>">
previous 10 products
</a>
受保护的字符串Prev10Url{
得到{
返回Convert.ToInt32(ViewData[“CurrentPage”])好吧,实际上我只尝试了两个链接[前10个和后10个]。我不确定你的寻呼机助手方法是否能帮上忙。不过,谢谢。好吧,让三元运算符变红真的很有帮助。现在看起来好多了,但是,没有其他方法可以进一步重构吗?除了控件或母版页中的封装之外,我建议使用强类型模型,而不是使用ViewData
除此之外,如果有意义的话,您还可以构建自己的HTML助手。