我不知道如何解决这个问题。在这种情况下,如果我只是使用排序,没有问题。只有在我向网站添加分页功能时才会出现问题(PS:我只尝试使用分页,但它仍然给我同样的错误)。
请帮帮我:)。
在控制器上:
public ActionResult Index(int? page, GridSortOptions SortModel)
{
userRsy = new UserRepository();
if (CookieUserName == "")
{
return RedirectToAction("Index", "Home");
}
else
{
DBEntities _db = new DBEntities();
//ViewData.Model = _db.Users;
ViewBag.SortModel = SortModel;
IEnumerable<User> userList = _db.Users;
if (!string.IsNullOrEmpty(SortModel.Column))
{
userList = userList.OrderBy(SortModel.Column, SortModel.Direction);
//userList = userList.OrderBy(u => u.UserName);
}
//find login user
var _user = userRsy.Find(CookieUserName);
//ViewBag.Title = _user.FirstName + ", Welcom to E-Tabs Dashboard Management Center";
//Check user admin level
ViewBag.User = _user.FirstName;
ViewBag.Account = _user.UserName;
ViewBag.IsAdmin = false;
if (userRsy.AdminCheck(_user.UserName) == 99)
{ ViewBag.IsAdmin = true; }
//Pagination
userList = userList.AsPagination(page ??1,5);
//show view
return View(userList);
}
}
在cshtml文件中:
@model IPagination<E_Tabs_Generic_Portal.Models.User>
@using MvcContrib.UI.Grid;
@using MvcContrib.Pagination;
@using MvcContrib.UI.Pager;
@using MvcContrib.Sorting;
@{
ViewBag.Title = "User";
Layout = "~/Views/Shared/_HomeLayout.cshtml";
}
<div class="content-1">
<h2>User List</h2>
@Html.Grid(Model).Sort(ViewBag.SortModel as GridSortOptions).Columns(column =>
{
column.For(p => p.UserName).Named("User Name");
column.For(p => p.IsEnabled).Named("Enabled");
column.For(p => p.FirstName).Named("First Name");
column.For(p => p.LastName).Named("Last Name");
column.For(p => p.Email).Named("E-Mail");
column.For(p => p.Company).Named("Company");
column.For(p => p.ServerName).Named("On Server #");
column.For(p => p.LastLoginTime).Named("Last Login").Format("{0:yyyy/MM/dd HH:mm}");
}
).Attributes( @class=>"Grid",@width =>"100%", @border =>"1", @style =>"text-align:center;border-collapse:collapse")
@Html.Pager(Model).First("First").Next(">>").Previous("<<").Last("Last").Format("Total{2}, Current{0}-{1} ")
</div>
答案 0 :(得分:0)
上个月我第一次尝试MVCCONTRIB网格时遇到了类似的问题。我通过将Ipagination结果分配给另一个变量并将其作为View返回来修复它。
这样的事情 -
var temp = userList.AsPagination(page ?? 1, 5);
return View( temp);