【问题标题】:Can anyone help me figure out pagination in MVC 3?谁能帮我弄清楚MVC 3中的分页?
【发布时间】:2012-04-03 22:33:19
【问题描述】:

我是 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 throws 和读取的错误

“Skip”方法仅支持 LINQ to Entities 中的排序输入。方法“OrderBy”必须在方法“Skip”之前调用。

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

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>

【问题讨论】:

  • 您是否尝试过按照错误的建议,将 orderby 放在 take 方法之前?
  • 你在IAMP = IAMP.OrderByDescending(p =&gt; p.PA).Skip(1);IAMP.OrderBy(p =&gt; p.PA).Skip(1);这两个地方都修复了吗?
  • 是的,我已经尝试将跳过放在这两个地方,但不幸的是我仍然遇到同样的错误????

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


【解决方案1】:

好的,经过大量的谷歌搜索,我想通了。该错误完全是我的错,因为在默认情况下我忘记添加

IAMP = IAMP.OrderBy(p =&gt; p.PA);

之前说的都是

IAMP.OrderBy(p =&gt; p.PA);

面手=脸+手掌

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 2011-08-15
    • 1970-01-01
    • 2019-11-30
    • 2011-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多