如何使用Viewmodel而不是ViewBag对网格进行排序?

时间:2016-12-20 03:31:04

标签: asp.net-mvc razor viewbag asp.net-mvc-viewmodel

我的代码正常工作并正确排序,但我正在使用ViewBag这样做。我想清理我的代码并使用ViewModel来对网格进行排序,但我很难理解如何操作。任何帮助将不胜感激。谢谢。

模型

public partial class Asset
    {
        public int AssetKey { get; set; }
        public int ProductKey { get; set; }
        public string InventoryOwner { get; set; }
        public string SerialNumber { get; set; }

控制器

public class InventoryManagementController : Controller
    {
        private Orders db = new Orders();


        public ActionResult Index(int? AssetNum, string keyword, string sortOrder, string currentFilter, int? page)
        {

            ViewBag.OwnerSort = sortOrder == "owner_asce" ? "owner_desc" : "owner_asce";
            ViewBag.AssetSort = sortOrder == "asset_asce" ? "asset_desc" : "asset_asce";
            ViewBag.SerialSort = sortOrder == "serialnum_asce" ? "serialnum_desc" : "serialnum_asce";
            ViewBag.ProductSort = sortOrder == "product_asce" ? "product_desc" : "product_asce";

            ViewBag.Keyword = keyword;
            var records = from s in db.Assets select s;
            string AssetNums = AssetNum.ToString();
            if (keyword != null)
            {
                page = 1;
            }
            else
            {
                keyword = currentFilter;
            }
            ViewBag.CurrentFilter = keyword;

            switch (sortOrder)
            {

                case "asset_asce":
                    records = records.OrderBy(s => s.AssetKey);
                    break;

                case "asset_desc":
                    records = records.OrderByDescending(s => s.AssetKey);
                    break;

                case "serialnum_asce":
                    records = records.OrderBy(s => s.SerialNumber);
                    break;

                case "serialnum_desc":
                    records = records.OrderByDescending(s => s.SerialNumber);
                    break; 

                case "product_asce":
                    records = records.OrderBy(s => s.Product.ProductName);
                    break;

                case "product_desc":
                    records = records.OrderByDescending(s => s.Product.ProductName);
                    break;

                case "owner_asce":
                    records = records.OrderBy(s => s.InventoryOwner);
                    break;

                case "owner_desc":
                    records = records.OrderByDescending(s => s.InventoryOwner);
                    break;

            int pageSize = 25; //Number of Records
            int pageNumber = (page ?? 1);

            return View(records.ToPagedList(pageNumber, pageSize));
            //return View(assets.ToList());
        }

查看

@model PagedList.IPagedList<OrderDB.Models.Asset>
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />

@{
    ViewBag.Title = "Inventory HomePage";
}

<!DOCTYPE html>

    <title>Index</title>
    <br />
   <h2>Inventory</h2>

        <table class="table" border="1">
            <tr>
                <th>
                    @Html.ActionLink("Asset Tag", "Index", new { sortOrder = ViewBag.AssetSort, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    @Html.ActionLink("Serial Number", "Index", new { sortOrder = ViewBag.SerialSort, keyword = ViewBag.Keyword, currentFilter = ViewBag.CurrentFilter })
                </th>
                <th>
                    @Html.ActionLink("Product", "Index", new { sortOrder = ViewBag.ProductSort, keyword = ViewBag.Keyword, currentFilter = ViewBag.CurrentFilter })
                </th>     
                <th>
                    @Html.ActionLink("Inventory Owner", "Index", new { sortOrder = ViewBag.OwnerSort, keyword = ViewBag.Keyword, currentFilter = ViewBag.CurrentFilter })
                </th>
            </tr>

            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.ActionLink(item.AssetKey.ToString(), "Details", new { id = item.AssetKey })
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.SerialNumber)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Product.ProductName)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.InventoryOwner)
                    </td>
                </tr>
            }
        </table>
        <br />
        Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount

        @Html.PagedListPager(Model, page => Url.Action("Index",
    new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

0 个答案:

没有答案