【问题标题】:the paging of Grid.MVC not workGrid.MVC 的分页不起作用
【发布时间】:2014-09-02 14:57:22
【问题描述】:

我在我的 MVC Web 应用程序中使用 Grid.MVC 当使用索引控制器在空白页面中对其进行测试时,它可以成功地进行分页和过滤。 当我把它放在我的项目中时,问题就出现了 我执行 ajax 请求(因为我不需要重新加载页面)到方法并返回包含 Grid.Mvc 搜索结果的部分视图的步骤结果和页面数成功返回但是当我按到下一页或过滤它不起作用。

代码:

查看:

@using (Ajax.BeginForm("Search", "Home",
        new AjaxOptions
        {
            HttpMethod = "POST",
            InsertionMode = InsertionMode.Replace,
            UpdateTargetId = "SearchResult"
        })){
@Html.DropDownList("Province", "Province")
@Html.DropDownList("Cities", "Cities")
<span>price from :</span>    <input type="text" name="Pricefrom" />
<span>to :</span>           <input type="text" name="Priceto" />
    <input type="submit" value="Search" />      }

搜索控制器:

   [HttpPost]
    public ActionResult Search(int? page , int Province = 0, int Cities = 0, int Pricefrom = 0, int Priceto = 0)
    {
        var ads = db.Ad.Where(a => (Cities == 0 || a.CityId == Cities) &&
                                   (Province == 0 || a.Cities.ProvinceId == Province)&&
                                   (Pricefrom == 0 || a.Price >= Pricefrom)&&
                                   (Priceto == 0 || a.Price <= Priceto)).OrderBy(a => a.AdDate).ToList();

        return PartialView("_Search", ads);
    }

部分视图:

@using GridMvc.Html
@model IEnumerable<Semsark.Areas.Backend.Models.Ad>
<div>
@Html.Grid(Model).Columns(columns =>
        {
            columns.Add(c => c.Id).Titled("ID");
            columns.Add(c => c.AdTitle).Titled("title");
            columns.Add(c => c.AdBody).Titled("body");
        }).WithPaging(2).Sortable(true)
</div>

视图 index.cshtml 中的脚本和样式:

<head>
<meta name="viewport" content="width=device-width" />
<link href="@Url.Content("~/Content/Gridmvc.css")" rel="stylesheet" />
<script src="@Url.Content("~/Scripts/gridmvc.min.js")"></script>
<script src="~/Scripts/gridmvc.lang.ru.js"></script>
<title>Index</title>

提前感谢您的帮助,

【问题讨论】:

  • 你在使用 Grid.Mvc.Ajax 包吗?

标签: ajax asp.net-mvc gridview kendo-grid


【解决方案1】:
@*Webgrid using Paging in mvc 4.

View Page **.cshtml**  *@

    @model MvcPopup.Models.PagedEmployeeModel

    @{
        //ViewBag.Title = "SearchEmployee";
        Layout = null;
    }

    @{
        WebGrid grid = new WebGrid(rowsPerPage: Model.PageSize);
        grid.Bind(Model.Employee,
                  autoSortAndPage: false,
                  rowCount: Model.TotalRows
        );
    }

    @grid.GetHtml(
        fillEmptyRows: false,
        alternatingRowStyle: "alternate-row",
        headerStyle: "grid-header",
        footerStyle: "grid-footer",
        mode: WebGridPagerModes.All,
                firstText: "<< First",
                previousText: "< Prev",
                nextText: "Next >",
                lastText: "Last >>",
        columns: new[] {


            grid.Column("Name",
                        header: "Name",
                        format: @<text>
                            @Html.ActionLink((string)item.Name, "ViewEmployeeDetail", new { id = item.id }, new { @class = "viewDialog" })</text>
                        ),
            grid.Column("Department"),
             grid.Column("City"),
              grid.Column("State"),
           grid.Column("Country",
                        header: "Country"
            ),
             grid.Column("Mobile"),
            grid.Column("",
                        header: "Actions",
                        format: @<text>
                            @Html.ActionLink("Edit", "EditEmployee", new { id = item.id }, new { @class = "editDialog" })
                            |

                            @Html.ActionLink("Delete", "Delete", new { id = item.id }, new { @class = "confirmDialog"})
                        </text>
            )
    })

@*-----------------------------------
Model folder under modelservices.cs file
=================================*@

    public IEnumerable<Employee> GetEmployeePage(int pageNumber, int pageSize, string searchCriteria)
            {
                if (pageNumber < 1)
                    pageNumber = 1;

                return db.Employees 
                    .OrderBy(m =>m.Name)
                  .Skip((pageNumber - 1) * pageSize)
                  .Take(pageSize)
                  .ToList();
            }
             public int CountAllEmployee()
            {
                return db.Employees.Count();
            }
            public class PagedEmployeeModel
        {
            public int TotalRows { get; set; }
            public IEnumerable<Employee> Employee { get; set; }
            public int PageSize { get; set; }
        }

@*Controller folder under create file like employeecontroller.cs *@

    using MvcPopup.Models;
    using System.Globalization;
    using System.Text;

    namespace MvcPopup.Controllers
    {
        public class EmployeeController : Controller
        {
            //
            // GET: /Employee/
            ModelServices mobjModel = new ModelServices();
            public ActionResult Index()
            {
                return View();
            }
            public ActionResult SearchEmployee(int page = 1, string sort = "name", string sortDir = "ASC")
            {
                const int pageSize = 5;
                var totalRows = mobjModel.CountAllEmployee(); 

                sortDir = sortDir.Equals("desc", StringComparison.CurrentCultureIgnoreCase) ? sortDir : "asc";

                var validColumns = new[] { "id", "name", "department", "country" };
                if (!validColumns.Any(c => c.Equals(sort, StringComparison.CurrentCultureIgnoreCase)))
                    sort = "id";

                var employee = mobjModel.GetEmployeePage(page, pageSize, "it." + sort + " " + sortDir);

                var data = new PagedEmployeeModel()
                {
                    TotalRows = totalRows,
                    PageSize = pageSize,
                    Employee = employee
                };
                return View(data);
            }
    ------------------------

【讨论】:

    【解决方案2】:

    您是否尝试过通过 IQueryable 列表而不是 IEnumerable?根据文档,Gridmvc.Html 需要一个 IQueryable 用于分页。 IQueryable 和 IEnumerable 之间存在一些细微的差异,这可能会有所不同。

    【讨论】:

      【解决方案3】:

      分页需要这些脚本:

       "~/Scripts/GridMvc/URI.js"
       "~/Scripts/GridMvc/gridmvc.min.js"
       "~/Scripts/GridMvc/gridmvc-ext.js"
      

      【讨论】:

        猜你喜欢
        • 2015-06-23
        • 1970-01-01
        • 2014-11-11
        • 2013-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多