2014-08-27 36 views
0

我想添加列排序鏈接到我的web應用程序中的「截止日期」列,我無法正確工作。目前,這是我在控制器代碼,我已經定義了switch語句來確定用戶想要如何排序:我想添加列排序鏈接到索引頁

var items = from s in db.Items 
        select s; 

     ViewBag.DateSortParm = sortOrder == "DueDate" ? "duedate_desc" : "DueDate"; 

     switch (sortOrder) 
     { 
      case "duedate_desc": 
       items = items.OrderByDescending(s => s.DueDate); 
       break; 
      case "DueDate": 
       items = items.OrderBy(s => s.DueDate); 
       break; 
      default: 
       items = items.OrderByDescending(s => s.DueDate); 
       break; 

在索引視圖這裏是我的代碼:

<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.First().ItemDescription) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.First().Quantity) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.First().Price) 
    </th> 
    <th> 
     @Html.ActionLink("Due Date", "Index", new 
     { 
      sortOrder=ViewBag.DateSortParm, 

     }) 

最後,這是我相信我得到我的錯誤,回到控制器,當我返回查看我有這樣的代碼:

int pageSize = 15; 
     int pageNumber = (page ?? 1); 
     pageNumber = page == null ? (ViewBag.pageData == null ? 1 : (int)ViewBag.pageData) : page.Value; 
     ViewBag.pageData = page; 
     ViewBag.searchBy = searchBy; 
     ViewBag.search = search; 
     return View(items.OrderBy(i => i.ItemId).ToPagedList(pageNumber,pageSize)); 

我要有.OrderBy去渲染,otherwi視圖如果我收到「跳過」方法錯誤。這是否導致問題,爲什麼列排序鏈接將無法正常工作?任何可能的解

- 謝謝!

回答

0

我想出了我的問題的答案。我必須刪除.OrderBy子句。