How to sort a Grid using Viewmodel not ViewBag?

142 views Asked by At

I have my code working and sorting correctly but I am using ViewBag to do so. I would like to clean up my code and use ViewModel instead to sort my grid but I'm stumped in how to do so. Any help would be greatly appreciated. Thank you.

Model

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

Controller

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());
        }

View

@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

There are 0 answers