我正在使用带有EF6的ASP.Net MVC,我在我的控制器中通过Skip和Take方法进行了分页
private const int PageSize = 6;
public JsonResult GetSearchingData(string SearchBy, string SearchValue, int page = 1)
{
Entities db = new Entities();
if (SearchBy == "Name")
{
ViewBag.CurrentPage = page;
ViewBag.PageSize = PageSize;
int p = page - 1;
ViewBag.CurrentPage = page;
ViewBag.PageSize = PageSize;
List<MyTable> SupList = new List<MyTable>();
SupList = db.MyTable.Where(x => x.Name.Contains(SearchValue) || SearchValue == null).ToList();
var subCategoryToReturn = SupList.Select(S => new { Name = S.Name });
ViewBag.TotalPages = Math.Ceiling(((double)subCategoryToReturn.Count()) / PageSize);
var temp =subCategoryToReturn.ToList().Skip(p * PageSize).Take(PageSize);
return Json(temp, JsonRequestBehavior.AllowGet);
}
else
return Json(new EmptyResult(), JsonRequestBehavior.AllowGet);
}
如何在JavaScript中添加分页链接?我试图在ajax中添加这个Razor代码来显示页码,但是不可能在ajax中添加Razor
@for(int i = 1; i <= ViewBag.TotalPages; i++) { <
a href = "@Url.Action("
GetSearchingData ","
MyControllerName ", new { page = i } )" > @i < /a>
}
$(document).ready(function() {
$("#Search").keydown(function() {
var SearchBy = $("#SearchBy").val();
var SearchValue = $("#Search").val();
//SearchData is the div name to append the result of ajax to html
var SetData = $("#SearchData");
SetData.html("");
$.ajax({
//some code
url: "/MyControllerName/GetSearchingData?SearchBy=" + SearchBy + "&SearchValue=" + SearchValue,
success: function(result) {
//Receive the filtering data from controller and show it for user
$.each(result, function(index, value) {
var Data = '<div class="col-sm-3" ><div class="card"><canvas id="header-blur"></canvas><div class="avatar"></div><div class="content"> <p>' +
value.Name + '</p></div></div></div>';
SetData.append(Data);
});
//here is my question how to add this Razor code to ajax ?
SetData.append(
@for(int i = 1; i <= ViewBag.TotalPages; i++) { <
a href = "@Url.Action("
GetSearchingData ","
MyControllerName ", new { page = i } )" > @i < /a>
}
);
}
}
});
});
});
剃刀格式化会变得非常棘手。在这种情况下,您必须尝试将所有这些保留在一行上。试试看:
@for(int i = 1; i <= ViewBag.TotalPages; i++)
{
@:"<a href = "@Url.Action("GetSearchingData ","MyControllerName ", new { page = i } )"> @i </a>";
}
编辑:如果它吐出编码的HTML,你也可以这样做:
@Html.Raw("<a href = \"" + Url.Action("GetSearchingData ","MyControllerName ", new { page = i } ) + "\"> @i </a>")