سؤال

I am using the latest RC2 version of MVC3.

I have a webgrid and it is giving me horrible problems, specifically with the paging and sorting. I have been told that the paging should be more efficient now, and not pull back the whole table but only rows needed for the page you are viewing. This is not working as i hoped (very slow), and so have taken it to the simplest form and booted up the profiler.

I have this ActionResult:

    public ActionResult TestGrid()
    {
        return View(ents.Decisions);
    }

And this view:

    @model IEnumerable<DecisionPanel.Web.Models.DataModel.Decision>

@{
    ViewBag.Title = "TestGrid";
    var usersGrid = new WebGrid(source: Model, rowsPerPage: 50);
}

<h2>TestGrid</h2>

@usersGrid.GetHtml(
        tableStyle: "grid",
        headerStyle: "header",
        alternatingRowStyle: "alt",
        rowStyle: "row",
                columns: usersGrid.Columns(
                        usersGrid.Column("UserID", "User Id"),
                        usersGrid.Column("HasAgreed", "Has Agreed?"),
                        usersGrid.Column("Comment"),
                        usersGrid.Column("DateResponded", "Date of Response", format: @<text>@item.DateResponded.ToString("dd MMM yyy (HH:mm.ss)")</text>)
        )
    )

Hitting the page is causing this to run on the profiler - 11 times:

SELECT 
[Extent1].[ID] AS [ID], 
[Extent1].[UserID] AS [UserID], 
[Extent1].[HasAgreed] AS [HasAgreed], 
[Extent1].[Comment] AS [Comment], 
[Extent1].[DateResponded] AS [DateResponded]
FROM [dbo].[DecisionResults] AS [Extent1]

I am having some other problems but if i can't even get this working i am considering abandoning the webgrid.

I know it's early days with it being out under a week, but has anyone else had any joy using the paging?

هل كانت مفيدة؟

المحلول

At this point I can confirm there is an issue with the WebGrid as shipping in MVC3 RC2 where it does not smartly handle IQueryable data that supports database-side paging. We'll look into resolving this for RTM. For now you would have to do paging manually. Sorry about that.

نصائح أخرى

Aren't you supposed to specify the pager similar to

@usersGrid.Pager(WebGridPagerModes.NextPrevious)

Kinda messed up that IQueryables are still not supported. I worked around it using a PagedList class

public class PagedList<T> : List<T>
{
    int _pageNr;
    int _pageSize;
    string _orderByCol;
    bool _asc;
    IQueryable<T> _query;

    public PagedList(int pageNumber, int pageSize, string orderBy, string direction, IQueryable<T> list)
    {
        var query = list;
        _query = list;
        if (!string.IsNullOrEmpty(orderBy))
        {
            if (!string.IsNullOrEmpty(direction))
            {
                orderBy = orderBy + " " + direction;
            }

            query = query.OrderBy(orderBy);
        }
        query = query.Skip(pageNumber * pageSize).Take(pageSize);
        this.AddRange(query.ToList());
    }

    public int Count 
    {
        get {
            return _query.Count();
        }
    }
}

Controller Code :

    public ActionResult Index(int page = 1, int pageSize = 2, string sort = null, string sortDir = "ASC")
    {
        return View(new PagedList<Something>(page-1, pageSize, sort, sortDir, Client.RetrieveAll<Something>()));
    }

With my view:

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
  <div>
    <%

        var grid = new WebGrid(rowsPerPage: 2);
       grid.Bind(
           Model,
           autoSortAndPage: false,
           rowCount: Model.Count
           );
       %>
    <%:
        grid.GetHtml()
    %>
  </div>
</asp:Content>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top