2012-04-12 39 views
4

有没有办法来重写默认MVC3 WebGrid排序行为来调用我的控制器(这将执行服务器端排序和返回数据)排序时调用?ASP.NET MVC3 WebGrid - 自定义,服务器端排序

感谢您的帮助!

+0

让我知道下面的答案是你在找什么。啊哈! – 2012-05-25 20:32:51

回答

9

您可以将服务器端的排序数据传递给webgrid,以及有多少记录的信息。 http://msdn.microsoft.com/en-us/magazine/hh288075.aspx有一个有用的演练。我正在做数据库级别的排序/过滤/分页,以尽量减少传递的数据量。我不认为我的网络服务器会爱我,如果我通过了客户所有的70,000个对象,所以他们可以看到第1页上的25。这几乎是你需要的一切,除了非常轻量级的视图模型,只是包装你的IEnumerable集合与一些额外的分页数据。

由于web网格只是使用查询字符串变量来决定要做什么,因此您需要使用get方法表单。你需要在查询字符串中包含排序字段/方向,以webgrid知道如何阅读它的方式。所以,你最终像本地主机/例子/管理/事情的URL thingName =凯悦&市= &状态= TX &国家= &排序=城市& sortdir = ASC

控制器:

public ActionResult Index(string thingName, string city, string state, string country, int page = 1) 
      { 
       const int pageSize = 25;     
       int totalRecords = 0; 
       IEnumerable<Thing> things = ThingModel.GetPagedSortedLocationsForCustomer(customerId, sort, sortdir, out totalRecords, pageSize, page, thingName, city, state, country); 

       PagedThings viewModel = new PagedThings(pageSize, page, totalRecords, things); 

       return View(viewModel); 
      } 

查看:

@model ExampleCompany.Area.ViewModels.PagedThings 

@{ 



using (Html.BeginForm("Index", "ThingaMaBob", System.Web.Mvc.FormMethod.Get)) 
{ 

    <label for="ThingName">ThingName</label>@Html.TextBox("ThingName", "") 
    <label for="City">City</label>@Html.TextBox("City", "") 
    <label for="State">State</label>@Html.TextBox("State", "") 
    <label for="Country">Country</label>@Html.TextBox("Country", "") 
    <input type="submit" value="Filter" /> 

    <br /> 
    var grid = new WebGrid(canPage: true, rowsPerPage: Model.PageSize, canSort: true); 
    grid.Bind(Model.Things, rowCount: Model.TotalRows, autoSortAndPage: false); 
    grid.Pager(WebGridPagerModes.All); 

     @grid.GetHtml(htmlAttributes: new { id = "grid"}, 
     columns: grid.Columns(
      //ommitted 
      grid.Column("thingName", "Thing"), 
     )); 

    Html.Hidden(grid.SortFieldName, grid.SortColumn); 
    Html.Hidden(grid.SortDirectionFieldName, grid.SortDirection == SortDirection.Ascending ? "ASC" : "DESC"); 
} 

型号:

public static IEnumerable<Thing> GetPagedSortedThingsForCustomer(int customerid, String sortby, String sorttype, out int totalRecords, int pageSize, int pageIndex, string thingName, string city, string state, string country) 
     { 
      var tmp = new List<Thing>(); 
      int total = 0; 

      dynamic dr = OurDBUtility.ReturnDR("ExampleProc_GetThingsSortedPaged", ConnectionInfo.ExampleConnection, customerid, sortby, sorttype, pageSize, pageIndex, thingName, city, state, country); 
      { 
       while (dr.Read()) 
       { 
        var tmpThing = new Thing(); 
        tmpThing.LoadFromDr(dr); 
        tmp.Add(tmpThing); 
        if (total == 0) 
        { 
         total = (int)dr["TOTAL_THINGS"]; 
        } 
       } 
      } 
      totalRecords = total; 

      return tmp; 
     } 

PROC动态SQL - 是的,你可以使用LINQ到SQL或如果你想其他的技术,但我的老同学:

CREATE PROCEDURE ExampleProc_GetThingsSortedPaged 
     (@CustomerId int 
     , @sortby nvarchar(60) 
     , @sorttype nvarchar(60) 
     , @pageSize int 
     , @pageIndex int 
     , @thingName nvarchar(255) = null 
     , @city nvarchar(30) = null 
     , @state nvarchar(30) = null 
     , @country char(2) = null 
    ) 
    as 

     DECLARE @strSql  nvarchar(3000) 
     --calculate paging rows 
     declare @startRow int, @endRow int 
     --e.g. if you have a page size of 10, page 1 = 1 - 10, page 2 = 11 -20 
     set @startRow = ((@pageIndex - 1) * @pageSize) + 1 
     set @endRow = @startRow + @pageSize - 1 

     if @thingName = '' 
      set @thingName = null 
     if @city = '' 
      set @city = null 
     if @state = '' 
      set @state = null 
     if @country = '' 
      set @country = null 

     --return total for webgrid, accounting for filter 
     declare @totalThings int 
     select @totalThings = COUNT(*) 
     from EXAMPLE_TABLE T with(nolock) 
     where CUSTOMER_ID = @CustomerId 
      AND (T.THING_NAME LIKE @thingName + '%' OR @thingName is null)    
      AND (T.CITY LIKE @city + '%' or @city is null) 
      AND (T.STATE LIKE @state + '%' or @state is null) 
      AND (T.COUNTRY = @country or @country is null) 



     DECLARE @ParameterDefinition AS NVARCHAR(200) 


     set @ParameterDefinition = '@totalThings int, @CustomerId INT, @startRow INT, @endRow INT, @thingName nvarchar(255), @city nvarchar(30), @state nvarchar(30), @country char(2)' 

     --When we need to do dynamic sql it is better to use paramterization, but you cannot do (ORDER BY @sortBy).  
     SET @strSql = N'SELECT * from 
         (
         select ROW_NUMBER() OVER (ORDER BY T.' + @sortby + ' ' + @sorttype + ') as Row, 
          @totalThings [TOTAL_THINGS], 
          T.THING_ID, T.THING_NAME, T.ADDRESS, T.CITY, T.STATE, 
          T.ZIP_CODE, T.COUNTRY 
         FROM EXAMPLE_TABLE T 
         WHERE T.CUSTOMER_ID = @CustomerId 
          AND (T.THING_NAME LIKE @thingName + ''%'' OR @thingName is null)    
          AND (T.CITY LIKE @city + ''%'' or @city is null) 
          AND (T.STATE LIKE @state + ''%'' or @state is null) 
          AND (T.COUNTRY = @country or @country is null) 
          ) paged 
         where Row between @startRow and @endRow 
         ORDER BY Row' 

     --print @strSql 
     EXECUTE sp_executesql @strSql, @ParameterDefinition, @totalThings, @CustomerId, @startRow, @endRow, @thingName, @city, @state, @country 


    GO 

PROC与CTE:

CREATE PROCEDURE ExampleProc_GetThingsSortedPaged 
    (@CustomerID int 
    , @sortby nvarchar(60) 
    , @sorttype nvarchar(60) 
    , @pageSize int = 25 
    , @pageIndex int = 1 
    , @thingName nvarchar(255) = null 
    , @city varchar(30) = null 
    , @state nvarchar(30) = null 
    , @country char(2) = null 
) 
as 



declare @startRow int 
declare @endRow int 

SET @startRow = ((@pageIndex - 1) * @pageSize) + 1; 
SET @endRow = @startRow + @pageSize - 1; 

set @sortby = replace(LOWER(@sortby), '_', '') 
SET @sorttype = LOWER(@sorttype) 

if @sorttype != 'asc' and @sorttype != 'desc' 
begin 
     set @sorttype = 'asc' 
end 

;with cte_things as (
     SELECT 
      CASE 
        WHEN @sortby ='country' AND @sorttype = 'asc' then row_number() over (order by C.COUNTRY_NAME ASC) 
        WHEN @sortby ='country' AND @sorttype = 'desc' then row_number() over (order by C.COUNTRY_NAME DESC) 

        WHEN @sortby ='state' AND @sorttype = 'asc' then row_number() over (order by STATE ASC) 
        WHEN @sortby ='state' AND @sorttype = 'desc' then row_number() over (order by STATE DESC) 

        WHEN @sortby ='city' AND @sorttype = 'asc' then row_number() over (order by CITY ASC) 
        WHEN @sortby ='city' AND @sorttype = 'desc' then row_number() over (order by CITY DESC) 

        WHEN @sortby ='thingname' AND @sorttype = 'desc' then row_number() over (order by THING_NAME DESC) 
        ELSE row_number() over (order by THING_NAME ASC) 
      END AS Row 
      ,T.THING_ID, T.THING_NAME, T.THING_TYPE, T.ADDRESS, T.CITY, T.STATE 
      , T.ZIP_CODE, T.COUNTRY_CODE, C.COUNTRY_NAME, T.PHONE_NUMBER 
      , T.LATITUDE, T.LONGITUDE 
      FROM EXAMPLE_TABLE L 
      join COUNTRIES C 
        on C.COUNTRY_CODE = L.COUNTRY_CODE 
      where 
        T.CUSTOMER_ID = @CustomerId 
        and L.CITY = ISNULL(@city, CITY) 
        and L.STATE = ISNULL(@state, STATE) 
        and L.COUNTRY_CODE = ISNULL(@country, L.COUNTRY_CODE) 
        and L.THING_NAME = ISNULL(@thingName, THING_NAME) 
) 
, cte_total as (select COUNT(*) as TOTAL_THINGS from cte_things) 
, cte_all as (select cte_things.*, cte_total.TOTAL_THINGS from cte_things cross join cte_total) 

SELECT * FROM cte_all 
where 
     Row >= @startRow 
     and Row <= @endRow 
ORDER BY Row 



GO 
+0

啊!我一直在寻找一个带有过滤和服务器端分页的WebGrid的例子。感谢那。 – Carl 2012-05-28 09:25:32

+0

我不断看到疯狂的例子,人们只是把一个“过滤器”文本框。显然,他们希望他们的用户使用他们的数据库列名或模型名称来输入过滤器。这对我来说似乎非常危险。无论如何,小的更新。我将摆脱动态sql并在.net中构建一个参数化查询,这样当我不在城市进行过滤时,我不必在城市的条件下进行硬编码。是的,你可以在动态sql中消除它,但它使@parameterDefinition更难处理。 – 2012-05-29 18:36:06

+0

向用户公开列名似乎很危险和不友善!但是你去了。关于DB位,我建议你在实体框架Linq上投入一些时间。我是你的老同学,所以我采取了一些说服,但我现在喜欢它。 – Carl 2012-05-30 08:54:33