-3

大家好,我的控制器中有以下代码:

 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
        &nbsp;
        @if (Model.HasPreviousPage)
        {

            @Html.ActionLink("<<", "Index", new { CounterForPage = 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter })
            @Html.Raw("&nbsp;");
            @Html.ActionLink("< Previous Page", "Index", new { CounterForPage = Model.PageNumber - 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.WordFilter })
        }
        else
        {
            @:<<
            @Html.Raw("&nbsp;");
            @:< Prev
        }
        &nbsp;
        @if (Model.HasNextPage)
        {
            @Html.ActionLink("Next Page >", "Index", new { CounterForPage = Model.PageNumber + 1, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter })
            @Html.Raw("&nbsp;");
            @Html.ActionLink(">>", "Index", new { CounterForPage = Model.PageCount, Ordering = ViewBag.CurrentSort, WordFilter = ViewBag.CurrentFilter })
        }
        else
        {
            @:Next>
            @Html.Raw("&nbsp;")
            @:>>
        }



    </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   
4

4 回答 4

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.

于 2012-04-09T22:14:45.020 回答
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可能是什么。

于 2012-04-09T22:11:41.520 回答
0

return 语句存在当前函数。return 语句之后的任何行都不会执行。

于 2012-04-09T22:11:59.423 回答
0

如何创建一个视图模型来保存结果和信息对象并将其返回给视图?

// 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 的所有调用,然后试一试。

于 2012-04-09T22:17:31.000 回答