在第一次单击按asc排序,在第二次单击desc asp.net mvc

时间:2015-03-27 08:52:36

标签: c# asp.net-mvc asp.net-mvc-4 sorting

我正在尝试通过表格的菜单项对我的视图进行排序。

这是我的控制器

 public ActionResult Index(string sortOrder)
        {
            ViewBag.NameSortParm = "name";
            ViewBag.descSortParm = "description";
            ViewBag.beginDateSortParm = "beginDate";
            ViewBag.endDateSortParm = "endDate";
            ViewBag.priceSortParm ="price";


            var persons = from s in db.PRT_FEE_PRICES
                          select s;
            switch (sortOrder)
            {
                case "name":
                    persons = persons.OrderBy(s => s.NAME);
                    break;
                case "description":
                    persons = persons.OrderBy(s => s.DESCRIPTION);
                    break;
                case "beginDate":
                    persons = persons.OrderBy(s => s.BEGIN_DATE);
                    break;
                case "endDate":
                    persons = persons.OrderBy(s => s.END_DATE);
                    break;
                case "price":
                    persons = persons.OrderBy(s => s.PRICE);
                    break;
                default:
                    persons = persons.OrderBy(s => s.NAME);
                    break;
            }

            return View(persons.ToList());
        }

和这里的观点

<table class="table">
    <tr>
        <th>
            @Html.ActionLink("სახელი", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>
        <th>
            @Html.ActionLink("აღწერა", "Index", new { sortOrder = ViewBag.descSortParm })
        </th>
        <th>
            @Html.ActionLink("დაწყების თარიღ", "Index", new { sortOrder = ViewBag.beginDateSortParm })
        </th>
        <th>
            @Html.ActionLink("დამთავრების თარიღი", "Index", new { sortOrder = ViewBag.endDateSortParm })
        </th>
        <th>
            @Html.ActionLink("ფასი", "Index", new { sortOrder = ViewBag.priceSortParm })
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.NAME)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DESCRIPTION)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.BEGIN_DATE)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.END_DATE)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.PRICE)
            </td>

        </tr>
    }

</table>

它完美地排序一切。我怎么能在第二次点击时通过降序对这些字段进行排序?我不知道该怎么做?

2 个答案:

答案 0 :(得分:0)

考虑将Controller方法上收到的值进行通勤,以便在升序或降序之间切换。

例如:

public ActionResult Index(string sortOrder)
{
    ViewBag.NameSortParm = sortOrder == "name" ? "name_desc" : "name";
    // Rest of your sortOrder param goes here...

    var persons = from s in db.PRT_FEE_PRICES
                  select s;
    switch (sortOrder)
    {
        case "name":
            persons = persons.OrderBy(s => s.NAME);
            break;
        case "name_desc":
            persons = persons.OrderByDescending(s => s.NAME);
            break;
        // ...
    }

    return View(persons.ToList());
}

我的回答是基于此link上的入门教程示例。

答案 1 :(得分:0)

您可以在switch循环中使用if..else循环并更改名称列表和对数据进行排序。

public ActionResult Index(string sortOrder)
{
    ViewBag.NameSortParm = sortOrder == "name" ? "name_desc" : "name";
    // Rest of your sortOrder param goes here...

    var persons = from s in db.PRT_FEE_PRICES
                  select s;
    switch (sortOrder)
    {
        case "name":
            if(sortOrder.Equals(CurrentSort))
        persons = persons.OrderBy(s => s.NAME);
           else
         persons = persons.OrderByDescending(s => s.NAME);
            break;
        `enter code here` // ...
    }

    return View(persons.ToList());
}

您的视图就在这里。

<table class="table">
    <tr>
        <th>
            @Html.ActionLink("სახელი", "Index", new { sortOrder = "name",  CurrentSort = ViewBag.NameSortParm })
        </th>
............
//enter your remaining code...//
.....