任何人都可以帮我搞清楚MVC 3中的分页吗?

时间:2012-04-02 15:25:30

标签: asp.net-mvc-3 sorting pagination

我是MVC和Stackoverflow的新手我正在尝试使用旧数据库实现MVC 3项目的分页。我已经完成了

的教程

http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

但是当我尝试实现分页方法时,它会编译,但它不会运行IE抛出和读取错误

只有LINQ to Entities中的排序输入才支持'Skip'方法。必须在方法'Skip'之前调用'OrderBy'方法。

我尝试在错误之前的行中将order之前和之后放置,但它仍然给出了同样的错误。我已经为我的控制器和索引发布了我的代码。如果有人之前看过这个错误并且可以查看我的代码,我将非常感激。

PaController

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

namespace DBFirstMVC.Controllers
{
    public class PaController : Controller
    {
        PaEntities db = new PaEntities();
        //
        // GET: /Pa/

        public ViewResult Index(string sortOrder, string currentFilter, string searchString, int? page)
        {
            ViewBag.CurrentSort = sortOrder;
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "PA desc" : "";

            if (Request.HttpMethod == "GET")
            {
                searchString = currentFilter;
            }
            else
            {
                page = 1;
            }
            ViewBag.CurrentFilter = searchString;


            var IAMP = from p in db.iamp_mapping select p;

            if (!String.IsNullOrEmpty(searchString))
            {
                IAMP = IAMP.Where(p => p.PA.ToUpper().Contains(searchString.ToUpper()));
            }

            switch (sortOrder)
            {
                case "Pa desc":
                    IAMP = IAMP.Skip(1).OrderByDescending(p => p.PA);
                    break;


                default:
                    IAMP.Skip(1).OrderBy(p => p.PA);
                    break;
            }
            int pageSize = 20;
            int pageNumber = (page ?? 1);

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


        //
        // GET: /Pa/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Pa/Create

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

        //
        // POST: /Pa/Create

        [HttpPost]
        public ActionResult Create(iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.iamp_mapping.Add(IAMP);
                    db.SaveChanges();
                }

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pa/Edit/5

        public ActionResult Edit(string id)
        {

            using (var db = new PaEntities())
            {

                return View(db.iamp_mapping.Find(id));
            }
        }

        //
        // POST: /Pa/Edit/5

        [HttpPost]
        public ActionResult Edit(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    db.Entry(IAMP).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("");
                }
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Pa/Delete/5

        public ActionResult Delete(string id)
        {
            using (var db = new PaEntities())
            {

                return View(db.iamp_mapping.Find(id));
            }
        }

        //
        // POST: /Pa/Delete/5

        [HttpPost]
        public ActionResult Delete(string id, iamp_mapping IAMP)
        {
            try
            {
                using (var db = new PaEntities())
                {
                    var vIAMP = db.iamp_mapping.Find(id);
                    db.Entry(vIAMP).State = EntityState.Deleted;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

            }
            catch (Exception e)
            {
                throw (e);
                //return View();
            }
        }




    }
}

Index.cshtml

@model PagedList.IPagedList<DBFirstMVC.Models.iamp_mapping>

@{
    ViewBag.Title = "Index";
}

@using PagedList;  
<h2>Index</h2>

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

@using (Html.BeginForm())
{
    <p>
        Find by PA: @Html.TextBox("SearchString")
        <input type = "submit" value = "Search" />l
    </p>

}
<table>
    <tr>
        <th>
            @Html.ActionLink("PA", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th>
            MAJOR PROGRAM
        </th>
        <th>
            INVESTMENT AREA
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.PA)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.MAJOR_PROGRAM)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.INVESTMENT_AREA)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.PA }) |

            @Html.ActionLink("Delete", "Delete", new { id=item.PA })
        </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 个答案:

答案 0 :(得分:1)

好的,所以我在大量的谷歌搜索后想出来了。错误完全是我的错,因为在默认情况下我忘了添加

IAMP = IAMP.OrderBy(p => p.PA);

之前所说的都是

IAMP.OrderBy(p => p.PA);

Facepalm =脸+手掌

感谢大家的帮助!