大家好我的控制器中有以下代码:
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
答案 0 :(得分:2)
您在此代码块中遗漏了一些内容:
{
var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
}
这样写,你只是在创建一个新的范围。对于您当前的代码,这相当于:
var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
并且总是让方法返回,从不运行switch
。您应该弄清楚该代码块应该做什么并调整您的代码。
另一件事:UserInfo.UserName.Equals(User.Identity.Name)
。这在C#中不是必需的,因为==
运算符为string
重载(与java无关,这是不可能的),因此您只需编写UserInfo.UserName == User.Identity.Name
。 / p>
答案 1 :(得分:1)
您将从此方法返回:
{
var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
}
在此之前你错过了if
测试吗?或者你真的想回到这里吗?
看起来你应该:
if (some condition)
{
var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
}
这是有意义的,因为它只返回一些时间,允许方法中的其余代码执行。我不知道some condition
可能是什么。
答案 2 :(得分:0)
return语句存在当前函数。 return语句后面的任何行都不会执行。
答案 3 :(得分:0)
如何创建一个viewmodel来保存结果和info对象并将其返回给视图?
// Example view model containing data for Info and pagenumberResults needed on the view
// Replace MyInfo with the type of object your info variable is.
public class MyViewModel
{
public MyInfo Info { get; set; }
public PageNumberResults Pages { get; set; }
}
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();
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);
if (PageNumberResults.Any())
{
return View(new MyViewModel()
{
Info = info,
PageNumberResults = FullDatabaseItem.Count()
});
}
return View("ErrorView");
}
然后在你的视图中,只需用Model.Info或Model.Pages等替换对Model的所有调用,并给它一个去。