我在MVC3應用程序中實現了分頁。點擊每個頁碼後會發生回傳,並且正確顯示請求的頁面。使用JS函數調用的MVC3中的分頁問題
下面是我的推廣代碼 -
public static string PageLinks(this HtmlHelper html, PageLinks pagelinks, Func<int, string> pageUrl)
{
StringBuilder result = new StringBuilder();
int startFrom;
//Previous Page link
int divi = (int)pagelinks.Current/pagelinks.PageLinksToDisplay;
if (divi > 0)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
tag.MergeAttribute("href", pageUrl((divi * pagelinks.PageLinksToDisplay) - 1));
tag.InnerHtml = "Prev";
result.AppendLine(tag.ToString());
}
if (divi > 0)
{
startFrom = (divi * pagelinks.PageLinksToDisplay);
}
else
{
startFrom = (divi * pagelinks.PageLinksToDisplay) + 1;
}
int endOn = (startFrom + pagelinks.PageLinksToDisplay);
if (endOn > pagelinks.Total + 1)
endOn = pagelinks.Total + 1;
for (int i = startFrom; i < endOn; i++)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
tag.MergeAttribute("href", pageUrl(i));
tag.InnerHtml = i.ToString();
if (i == pagelinks.Current)
tag.AddCssClass("selected");
result.AppendLine(tag.ToString());
}
if (endOn < pagelinks.Total)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
tag.MergeAttribute("href", pageUrl(endOn));
tag.InnerHtml = "Next";
result.AppendLine(tag.ToString());
}
return result.ToString();
}
下面是源代碼,我查看實現分頁 -
<!-- Paging Strip -->
<div class="PagerDiv">
<div class="pagerCurrent">
<%
int? currentPgNo = Model.Pager.Current;
int totalItems = Model.Pager.TotalItemCount;
int displayRecs = Model.Pager.PageSize;
%>
Showing <%=(currentPgNo * displayRecs) - (displayRecs - 1)%> -
<%=(currentPgNo * displayRecs < totalItems ? currentPgNo * displayRecs : totalItems)%>
out of <%=totalItems%>
</div>
<div class="pager">
<%=Html.PageLinks(Model.Pager, x => Url.Action("Index", new { Current = x }))%>
</div>
<div class="options">
Sort By : <%:Html.DropDownListFor(m=>m.SortBy, Model.SortOptions, new { onchange = "this.form.submit();" })%>
</div>
</div>
<!-- End Paging-->
下面是輸出 -
問題: -
到目前爲止好。我正面臨新的困難。 我有一個視圖,其中數據通過json呈現。我把我的部分觀點之一渲染爲Json。該部分視圖顯示了需要執行分頁的列表。我不想回發,而是需要在Div中顯示新呈現的json。
代碼
<%=Html.PageLinks(Model.Pager, x => Url.Action("Index", new { Current = x }))%>
實際生成編號的箱子,我想是附加在這些編號的箱子,而不是URL的JS功能。
我試着像
<%=Html.JQueryPageLinks(Model.Pager, x => "MyJSFunction(" + x + ")")%>
其中x代表頁碼。創建另一個擴展,但似乎沒有運氣。
這裏是代碼 -
public static string JQueryPageLinks(this HtmlHelper html, PageLinks pagelinks, string jQryFn)
{
StringBuilder result = new StringBuilder();
int startFrom;
//Previous Page link
int divi = (int)pagelinks.Current/pagelinks.PageLinksToDisplay;
if (divi > 0)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
//tag.MergeAttribute("href", pageUrl((divi * pagelinks.PageLinksToDisplay) - 1));
tag.Attributes.Add("onclick", jQryFn);
tag.InnerHtml = "Prev";
result.AppendLine(tag.ToString());
}
if (divi > 0)
{
startFrom = (divi * pagelinks.PageLinksToDisplay);
}
else
{
startFrom = (divi * pagelinks.PageLinksToDisplay) + 1;
}
int endOn = (startFrom + pagelinks.PageLinksToDisplay);
if (endOn > pagelinks.Total + 1)
endOn = pagelinks.Total + 1;
for (int i = startFrom; i < endOn; i++)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
//tag.MergeAttribute("href", pageUrl(i));
tag.Attributes.Add("onclick", jQryFn);
tag.InnerHtml = i.ToString();
if (i == pagelinks.Current)
tag.AddCssClass("selected");
result.AppendLine(tag.ToString());
}
if (endOn < pagelinks.Total)
{
TagBuilder tag = new TagBuilder("a"); // Construct an <a> tag
//tag.MergeAttribute("href", pageUrl(endOn));
tag.Attributes.Add("onclick", jQryFn);
tag.InnerHtml = "Next";
result.AppendLine(tag.ToString());
}
return result.ToString();
}
有人可以幫助我解決這問題。 感謝您分享寶貴的時間和智慧。
編輯:最簡單的我想JS頁面編號而不是網址的JS函數調用。 例如。
點擊1將調用MyJSFunction(1),點擊 2將調用MyJSFunction(2) 等等...
看來你還沒有理解我的要求。您提供的代碼是通過ajax提交表單。請檢查我的問題的編輯。 – IrfanRaza 2013-03-13 10:33:50