CONTROLLER
我正在为表添加分页和排序。 用户变量已成功填充记录列表。我的问题是这段代码给出了“在App_Web_aa2wbzst.dll中发生了类型'System.NullReferenceException'的异常,但未在用户代码中处理”。 这是视图的前两行。
private UserBs objBs;
public ListUserController()
{
objBs = new UserBs();
}
// GET: Admin/ListUser
public ActionResult Index(String SortOrder, String SortBy, String Page)
{
ViewBag.SortOrder = SortOrder;
ViewBag.SortBy = SortBy;
var users = objBs.GetALL();
switch (SortBy)
{
case "UserEmail":
switch (SortOrder)
{
case "Asc":
users = users.OrderBy(x => x.UserEmail).ToList();
break;
case "Desc":
users = users.OrderByDescending(x => x.UserEmail).ToList();
break;
default:
break;
}
break;
case "Role":
switch (SortOrder)
{
case "Asc":
users = users.OrderBy(x => x.Role).ToList();
break;
case "Desc":
users = users.OrderByDescending(x => x.Role).ToList();
break;
default:
break;
}
break;
default:
users = users.OrderBy(x => x.UserEmail).ToList();
break;
}
return View();
}
}
查看
<table class="table">
<tr>
<th>
@Html.ActionLink("UserEmail", "Index", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "UserEmail", page = (ViewBag.Page == null ? "1" : ViewBag.Page) })
</th>
<th>
@Html.ActionLink("Role", "Index", new { SortOrder = (ViewBag.SortOrder == null ? "Asc" : (ViewBag.SortOrder == "Asc" ? "Desc" : "Asc")), SortBy = "Role", page = (ViewBag.Page == null ? "1" : ViewBag.Page) })
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserEmail)
</td>
<td>
@Html.DisplayFor(modelItem => item.Role)
</td>
</tr>
}
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
您的视图需要模型
@foreach (var item in Model) {
但是你没有从控制器传递模型:
return View();
请改为尝试:
return View(users);