Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:40:27+00:00 2026-06-04T04:40:27+00:00

Why is it it gives error in return View(contacts.ToPagedList(pageNumber, pageSize)); statement the error in

  • 0

Why is it it gives error in return View(contacts.ToPagedList(pageNumber, pageSize)); statement the error in the Index method :
The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PhoneBook.Models;
using PagedList;

namespace PhoneBook.Controllers
{ 
public class ContactsController : Controller
{
    private PhoneDBContext db = new PhoneDBContext();

    //
    // GET: /Contacts/

    public ViewResult Index(string searchString, string sortOrder, Contact model, string currentFilter, int? page)
    {
        ViewBag.CurrentSort = sortOrder;

        ViewBag.FNameSortParm = sortOrder == "FName asc"? "FName desc" : "FName asc";
        ViewBag.DateSortParm = sortOrder == "Date asc" ? "Date desc" : "Date asc";
        ViewBag.LNameSortParm = sortOrder == "LName asc" ? "LName desc" : "LName asc";
        ViewBag.CompSortParm = sortOrder == "Company asc" ? "Company desc" : "Company asc";
        ViewBag.MobSortParm = sortOrder == "Mob asc" ? "Mob desc" : "Mob asc";
        ViewBag.TelSortParm = sortOrder == "Tel asc" ? "Tel desc" : "Tel asc";

        if (Request.HttpMethod == "GET") { searchString = currentFilter; }
        else {page = 1;}
        ViewBag.CurrentFilter = searchString;
        var contacts = from m in db.Contacts
                       select m;

        switch (sortOrder)
        {
            case "FName desc":
                contacts = contacts.OrderByDescending(s => s.FirstName);
                break;
            case "FName asc":
                contacts = contacts.OrderBy(s => s.FirstName);
                break;
            case "LName desc":
                contacts = contacts.OrderByDescending(s => s.LastName);
                break;
            case "LName asc":
                contacts = contacts.OrderBy(s => s.LastName);
                break;
            case "Company desc":
                contacts = contacts.OrderByDescending(s => s.Company);
                break;
            case "Company asc":
                contacts = contacts.OrderBy(s => s.Company);
                break;
            case "Date desc":
                contacts = contacts.OrderByDescending(s => s.DateAdded);
                break;
            case "Date asc":
                contacts = contacts.OrderBy(s => s.DateAdded);
                break;
            case "Mob desc":
                contacts = contacts.OrderByDescending(s => s.MobileNumber);
                break;
            case "Mob asc":
                contacts = contacts.OrderBy(s => s.MobileNumber);
                break;
            case "Tel desc":
                contacts = contacts.OrderByDescending(s => s.TelephoneNumber);
                break;
            case "Tel asc":
                contacts = contacts.OrderBy(s => s.TelephoneNumber);
                break;
        }


        if (!String.IsNullOrEmpty(searchString))
        {
            contacts = contacts.Where(s => s.LastName.ToUpper().Contains(searchString)||s.FirstName.ToUpper().Contains(searchString)||s.Company.ToUpper().Contains(searchString));
        }

        int pageSize = 3;
        int pageNumber = (page ?? 1);

       return View(contacts.ToPagedList(pageNumber, pageSize));
    }

    //
    // GET: /Contacts/Details/5

    public ViewResult Details(int id)
    {
        Contact contact = db.Contacts.Find(id);
        return View(contact);
    }

    //
    // GET: /Contacts/Create

    public ActionResult Create()
    {
        return View();
    } 

    //
    // POST: /Contacts/Create

    [HttpPost]
    public ActionResult Create(Contact contact)
    {
        if (ModelState.IsValid)
        {
            db.Contacts.Add(contact);
            contact.DateAdded = DateTime.Now;
            db.SaveChanges();
            return RedirectToAction("Index");  
        }

        return View(contact);
    }

    //
    // GET: /Contacts/Edit/5

    public ActionResult Edit(int id=0)
    {

        Contact contact = db.Contacts.Find(id);

        if (contact == null)    { return HttpNotFound(); } // returns blank page if id is not valid
        return View(contact);
    }

    //
    // POST: /Contacts/Edit/5

    [HttpPost]
    public ActionResult Edit(Contact contact)
    {
        if (ModelState.IsValid)
        {


            db.Entry(contact).State = EntityState.Modified;
            contact.DateAdded = DateTime.Now;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(contact);
    }

    //
    // GET: /Contacts/Delete/5

    public ActionResult Delete(int id)
    {
        Contact contact = db.Contacts.Find(id);
        if (contact == null) { return HttpNotFound(); }
        return View(contact);
    }

    //
    // POST: /Contacts/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {            
        Contact contact = db.Contacts.Find(id);
        if (contact == null) { return HttpNotFound(); }
        db.Contacts.Remove(contact);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    public ActionResult SearchIndex(string searchString)
    {
        var contacts = from m in db.Contacts
                     select m;

        if (!String.IsNullOrEmpty(searchString))
        {
            contacts = contacts.Where(s => s.LastName.Contains(searchString));
        }

        return View(contacts);
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }


}
}

below is the Index.cshtml code:

@model PagedList.IPagedList<PhoneBook.Models.Contact>
@{
    ViewBag.Title = "Phone Book";
}

<p>

     @using (Html.BeginForm()){   
         <p> Search: @Html.TextBox("SearchString", ViewBag.CurrentFilter as string) 
         <input type="submit" value="Go" /></p>
        }
</p>


<p>
    @Html.ActionLink("Create New", "Create")
</p>

<table>
    <tr>
        <th>
             @Html.ActionLink("First Name", "Index", new { sortOrder=ViewBag.FNameSortParm, currentFilter=ViewBag.CurrentFilter })
        </th>
        <th>
             @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.LNameSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("Mobile Num", "Index", new { sortOrder = ViewBag.MobSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.ActionLink("Tel Num", "Index", new { sortOrder = ViewBag.TelSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
             @Html.ActionLink("Company", "Index", new { sortOrder = ViewBag.CompSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
           @Html.ActionLink("Date Added/Updated", "Index", new { sortOrder = ViewBag.DateSortParm, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.FirstName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.LastName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MobileNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.TelephoneNumber)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Company)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.DateAdded)
        </td>
        <td>
            @Html.ActionLink("Details", "Details", new { id=item.ID })
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) 
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}

</table>

<div>
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber)
    of @Model.PageCount

    @if (Model.HasPreviousPage)
    {
        @Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  })
        @Html.Raw(" ");
        @Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  })
    }
    else
    {
        @:<<
        @Html.Raw(" ");
        @:< Prev
    }

    @if (Model.HasNextPage)
    {
        @Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  })
        @Html.Raw(" ");
        @Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  })
    }
    else
    {
        @:Next >
        @Html.Raw(" ")
        @:>>
    }
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:40:28+00:00Added an answer on June 4, 2026 at 4:40 am

    Try the following three changes.

    1. Put this code before the switch statement:

      if (!String.IsNullOrEmpty(searchString))
      {
          contacts = contacts.Where(s => s.LastName.ToUpper().Contains(searchString)||s.FirstName.ToUpper().Contains(searchString)||s.Company.ToUpper().Contains(searchString));
      }
      
    2. Add a default case in your switch statement, and make it throw.

      switch (sortOrder) {
          case ...:
              ...
          default:
              throw new ArgumentException("Bad sort order specified", "sortOrder");
      }
      
    3. Use the type IOrderedQueryable<T>.

      IOrderedQueryable<T> orderedContacts;
      switch (sortOrder)
      {
          case "FName desc":
              orderedContacts = contacts.OrderByDescending(s => s.FirstName);
              break;
          ...
      }
      
      ...
      
      return View(orderedContacts.ToPagedList(pageNumber, pageSize));
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have public ActionResult Index() { ViewBag.TotalRecords = _tabmasterService.Count(); return View(_tabmasterService.GetTabMasterList(10, 1)); } now
Android app gives me an error now: package com.martijngijselaar.rooster; import android.os.Bundle; import android.view.MotionEvent; import
the second function gives error C2803 http://msdn.microsoft.com/en-us/library/zy7kx46x%28VS.80%29.aspx : 'operator ,' must have at least
i added this line.but it gives error.how to import it? import org.andengine.opengl.texture.atlas.TextureAtlas;
The cake php validation 'isUnique' gives error on edit var $validate = array( 'name'
I have following asp.net code but it gives error when I change dropdown selected
The offending line str.replace(/ /g, ) gives Error: In orders.js.erb.coffee, Parse error on line
How could i validate a textbox in vb.net, so that it gives error message
When I am executing this code in http://www.pyschools.com/quiz/view_question/s2-q1 . It gives error for both
My program gives this error under gdb: During startup program exited with code 0xc0000135.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.