-3

Hi people i have the following code in my controller:

 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");
        }

Paging works when i remove the following code:

`var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();`
                return View(Info);

but there is a green underline under the switch (ordering) which says:

Unreachable Code Dectected

I need both as they are part of my application. Please can you provide me some help thank you for your kind efforts

My view Code:

    @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>

Created Class:

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; }

    }
}

Errors from class:

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   
user1137472
  • 345
  • 2
  • 5
  • 20
  • 3
    Doesn't `return View(Info);` always get run, thus making the rest unreachable? – Mike Christensen Apr 09 '12 at 22:11
  • so ur suggesting to move the return view (info) – user1137472 Apr 09 '12 at 22:15
  • Well, you have two unconditional return statements in that method.. So obviously you need to sprinkle in some `if` statements somewhere.. – Mike Christensen Apr 09 '12 at 22:17
  • 1
    Could you please provide me a method of where these if statements go as i havent used if statements before – user1137472 Apr 09 '12 at 22:37
  • Perhaps post some of your view code as well to show the usage as it looks to me like your passing two different objects into your view which would indicat perhaps it's not strongly typed (assumption of course)? – dreza Apr 09 '12 at 23:14

4 Answers4

2

You are missing something at this code block:

{
    var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
    return View(Info);
}

Written like this, you are just creating a new scope. For your current code, this is equivalent to:

  var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
  return View(Info);

And will always make the method return, never running your switch. You should figure out what that code block was supposed to do and adjust your code.

Another thing: UserInfo.UserName.Equals(User.Identity.Name). This isn't necessary in C#, as the == operator is overloaded for string (in constrast to java, where this isn't possible), so you can just write UserInfo.UserName == User.Identity.Name.

Femaref
  • 60,705
  • 7
  • 138
  • 176
  • That code works fine its been tested without the paging and is working fine just when i add the paging it throws an error – user1137472 Apr 09 '12 at 22:25
  • Yes, because if you remove the paging, the `return` detailed in my post is the last line in the code. If you add it, the `return` will execute regardless and the error will appear. – Femaref Apr 09 '12 at 23:00
  • I have tried your answer, the first one it did the same thing saying it could not be reached thanks for ur efforts – user1137472 Apr 10 '12 at 00:13
1

You are returning from the method here:

{
    var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
    return View(Info);
}

Are you missing an if test before this? or do you really want to return here?

It looks like you should have:

if (some condition)
{
    var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
    return View(Info);
}

This would make sense as it would only return some of the time, allowing the rest of the code in the method to execute. I have no idea what some condition could be.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
  • as long as the code works as it did without the paging i dont mind what ever method you provide. As the code shown here makes the games unique to the user. So as long as it still does that am fine with what ever method thanks – user1137472 Apr 09 '12 at 22:17
  • That is what I thought too, it even seems like there was a blank space for some sort of test condition. – Travis J Apr 09 '12 at 22:17
  • Could you provide an answer with if statements as i am not filmiar with how to use them thank you – user1137472 Apr 09 '12 at 22:32
  • @user1137472 - I don't know what you want to achieve with this code, so an example would be fairly pointless - but the syntax is `if () { }` – ChrisF Apr 09 '12 at 22:36
  • The first code in ur answer is there to make all the games that user post unique and i know the syntax of an if but i have no idea where to impliment them in my solution, i really would be greatfull if you could provide a solution for me thanks been struggling on this for ages – user1137472 Apr 09 '12 at 22:39
  • I really dont know what condition i am suppose to put in like i said i have no idea how to use if statements – user1137472 Apr 09 '12 at 22:47
  • Sorry, we don't know what your code is supposed to do. You'll have to decide/know that and write the code. – Femaref Apr 09 '12 at 23:02
0

the return statement exists the current function. any lines after the return statement will not execute.

Sam Axe
  • 33,313
  • 9
  • 55
  • 89
0

How about creating a viewmodel to hold the results and info object and return that to the view?

// 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");
    }

Then in your view just replace all calls to Model with Model.Info or Model.Pages etc and give that a go.

dreza
  • 3,605
  • 7
  • 44
  • 55
  • I dont want to go to the hassle of creating a view model messing with my controller and changing code as it will be a lot of hassel is there no way to adpat what i have – user1137472 Apr 09 '12 at 22:22
  • I will then also chang emy views realted to this as i have added addtional jquery code this will be a time hassling experiance – user1137472 Apr 09 '12 at 22:23
  • @user1137472 is your view not strongly typed? Perhaps if you posted some of your view code it might help. The simpliest answer to your question is to remove the first return View as that is the problem. However perhaps if you posted what you want to achieve we could offer another alternative? – dreza Apr 09 '12 at 23:13
  • That line of code var Info = db.tblGames.Include(x => x.tblConsole).Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList(); return View(Info); is there to make all games unique to the user who adds them no other that logs in can see what the other user has added. And yes i have a strongly typed view and tryied your answer i got errors saying myviewmodel does not exsit while i had called it exaactly that – user1137472 Apr 10 '12 at 00:06
  • I have tried to impliment your answer again, the controller code is fine at the momment but the class is showing errors. I will post what i did and the errors in edit and please can you see what I am doing wrong – user1137472 Apr 10 '12 at 00:28
  • @user1137472 The ViewModel I created was just an example. You need to put the correct types of the objects for Info and PageNumberResults rather than use my example ones (MyInfo etc) into your version. I also missed the property name for PageNumberResults so amended that. – dreza Apr 10 '12 at 02:47