English 中文(简体)
MVC 3 RC2 Webgrid nightmares, anyone have paging working correctly?
原标题:

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>




相关问题
How to change the default sort order to descending?

How do I change the defaultSort of my webGrid to be in the opposite/descending order? If it were SQL, I d be adding a DESC somewhere. Here s my working line of code for an Ascending sort: var grid =...

MVC 3 Webgrid Column

I m working on a MVC 3 webgrid at the moment, in one of the columns I wish to have a button, I ve achieved this when putting the following code in the view. @grid.GetHtml(columns: grid....

How to hide header on MVC3 WebGrid

Is there some easy way to hide header for MVC3 WebGrid extension? Something like var grid = new WebGrid(Model, canSort:false, canPage:false, showHeader:false); I can probably set css style for ...

asp.net mvc 3 webgrid sorting remains ?sortdir=ASC

I m experimenting a bit with several grids in asp.net mvc. Microsoft has a grid too know in the prerelease of mvc 3 so I thought I ll try that one out. The basic functionality is quite easy to ...

How to make a MVC 3 Webgrid with checkbox column?

The code below will insert an actionlink into one of the web grids columns. @{ View.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; var usersGrid = new WebGrid(source: ...

Mvc 3 texbox in webgrid (razor)

Simple Q:How do you I get the textbox to show the value. Code below fail on item.LastName @model List<Mvc2010_11_12.Models.Employee> @{ var grid = new WebGrid(source: Model,defaultSort: "...

ASP.NET MVC 3 WebGrid paging issue

My data access layer returns collection with rows for single page and total number of rows. Unfortunately WebGrid component does not allow to specify total number of rows or total number of pages (...

热门标签