大家好,我的控制器中有以下代码:
public ViewResult Index(string Ordering, int? CounterForPage)
{
var FullDatabaseItem = from b in db.tblGames
select b;
{
var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
}
switch (Ordering)
{
case "HeadlineName":
FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.GameName);
break;
case "DatePosted":
FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.ReleaseYear);
break;
case "DiscriptionDate":
FullDatabaseItem = FullDatabaseItem.OrderBy(b => b.ReleaseYear);
break;
default:
FullDatabaseItem = FullDatabaseItem.OrderByDescending(b => b.ReleaseYear);
break;
}
int pageSize = 3;
int pageNumber = (CounterForPage ?? 1);
var PageNumberResults = FullDatabaseItem.ToPagedList(pageNumber, pageSize);
ViewBag.PageNumberResults = FullDatabaseItem.Count();
if (PageNumberResults.Any())
{
return View(PageNumberResults);
}
return View("ErrorView");
}
当我删除以下代码时,分页工作:
`var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();`
return View(Info);
但是下面有一条绿色下划线switch (ordering)
,上面写着:
Unreachable Code Dectected
我需要两者,因为它们是我的应用程序的一部分。请你能给我一些帮助谢谢你的努力
我的观点代码:
@model PagedList.IPagedList<Games.Models.tblGame>
@{
ViewBag.Title = "Index";
}
@*<h2>Index</h2>*@
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
@* <tr>*@
@* <th>
GameID
</th>*@
@* <th>
GameName
</th>
<th>
ReleaseYear
</th>
<th>
Cost
</th>
<th>
Description
</th>
<th>
Downloads
</th>
<th>
Image
</th>
<th>
tblConsole
</th>*@
@* <th>
UserName
</th>*@
@* <th></th>
</tr>*@
@foreach (var item in Model) {
<tr>
@* <td>
@Html.HiddenFor(modelItem => item.GameID)
</td>*@
<td id = "TableLayout1">
<img width="100" height="100"alt="ImageFromDatabase" src='@item.Image' />
</td>
<td id = "TableLayout2">
@*@Html.DisplayFor(modelItem => item.GameName)*@
@Html.ActionLink(item.GameName, "Details", new { id = item.GameID })
</td>
<td id = "TableLayout3">
@Html.DisplayFor(modelItem => item.ReleaseYear)
</td>
<td id = "TableLayout4">
@Html.Raw(item.Description.Substring(0, item.Description.IndexOf(".") + 1))
@* @Html.DisplayFor(modelItem => item.Description)*@
</td>
<td id = "TableLayout5">
@Html.DisplayFor(modelItem => item.Cost)
</td>
<td id = "TableLayout6">
@Html.DisplayFor(modelItem => item.Downloads) @*want this as a link so I can then click on it and show the game downloads*@
</td>
<td id = "TableLayout7">
@Html.DisplayFor(modelItem => item.tblConsole.ConsoleName)
</td>
@* <td>
@Html.HiddenFor(modelItem => item.UserName)
</td>*@
<td id = "TableLayout8">
@Html.ActionLink("Edit", "Edit", new { id=item.GameID }) |
@Html.ActionLink("Details", "Details", new { id = item.GameID }) |
@Html.ActionLink("Delete", "Delete", new { id = item.GameID })
</td>
</tr>
}
</table>
<div class="PageCounter">
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
of @Model.PageCount
@if (Model.HasPreviousPage)
{
@Html.ActionLink("<<", "Index", new { CounterForPage = 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter })
@Html.Raw(" ");
@Html.ActionLink("< Previous Page", "Index", new { CounterForPage = Model.PageNumber - 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter })
}
else
{
@:<<
@Html.Raw(" ");
@:< Prev
}
@if (Model.HasNextPage)
{
@Html.ActionLink("Next Page >", "Index", new { CounterForPage = Model.PageNumber + 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter })
@Html.Raw(" ");
@Html.ActionLink(">>", "Index", new { CounterForPage = Model.PageCount, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter })
}
else
{
@:Next>
@Html.Raw(" ")
@:>>
}
</div>
<script type="text/javascript">
var uvOptions = {};
(function () {
var uv = document.createElement('script'); uv.type = 'text/javascript'; uv.async = true;
uv.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'widget.uservoice.com/ZRhsC1RL1m4gK5megTxxlw.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(uv, s);
})();
</script>
创建类:
namespace Games.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web;
using PagedList;
using System.Web.Mvc;
public class MyViewModel
{
public MyInfo Info { get; set; }
public PageNumberResults { get; set; }
}
}
课堂上的错误:
Error 1 Invalid token '{' in class, struct, or interface member declaration
Error 2 Invalid token ';' in class, struct, or interface member declaration
Error 3 Invalid token ';' in class, struct, or interface member declaration
Error 4 Type or namespace definition, or end-of-file expected