使用当前View的模型对表进行排序,不带插件

时间:2014-06-30 14:35:51

标签: asp.net-mvc sorting model-view-controller model sql-order-by

我还是MVC的新手,我正试图找到一种方法来正确排序我当前的View数据而无需安装插件。

现在,当我点击一个排序(只是一个按列标题的向上和向下箭头按钮)时,它会调用一个查询并重新提取我拥有的所有数据,但使用的是另一个OrderBy这样做很糟糕。

这是一种糟糕的排序方式,因为我正在调用我在View中已有的数据,但我不知道如何将当前信息从View传递给Controller。

问:如何在我的视图中获取当前数据并使用我的控制器对其进行排序?最好不必安装额外的插件并在我的Index方法中完成。

资源

图像表示 enter image description here

控制器中的索引。

注意:我有一个FilterBy和Sort选项。我的控制器索引有三个if选项,第一个是如果没有调用选项,第二个是调用Filter,第三个是调用Sort。正如您所看到的,Sort只是一个包含10个case语句的查询,这些语句都会调用数据并按不同的顺序排序。

public ActionResult Index(string Filter, string searchString, string Sort)
{
    ViewBag.champ1 = new SelectList(db.championLists, "ID", "Name");
    ViewBag.champ2 = new SelectList(db.championLists, "ID", "Name");
    ViewBag.champ3 = new SelectList(db.championLists, "ID", "Name");
    ViewBag.elo = new SelectList(db.eloLists, "ID", "rank");
    ViewBag.rank = new SelectList(db.rankLists, "ID", "rank");
    ViewBag.role1 = new SelectList(db.roleLists, "ID", "lane");
    ViewBag.role2 = new SelectList(db.roleLists, "ID", "lane");

    // NEITHER FILTER NOR SORT
    if (String.IsNullOrEmpty(Sort) && (String.IsNullOrEmpty(searchString) || String.IsNullOrEmpty(Filter)))
    {
        return View(db.Users.ToList().OrderBy(x => x.name));
    }   else

        // FILTER STATEMENT
        if ((!String.IsNullOrEmpty(searchString) && !String.IsNullOrEmpty(Filter)) && (String.IsNullOrEmpty(Sort)))
        {
            var parameter = 1;
            try
            {
                parameter = Int32.Parse(Filter);
            }
            catch
            {

            }
            return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                .Where(x => (parameter == 1 && x.name.Contains(searchString)) ||
                    (parameter == 2 && x.school.Contains(searchString)) ||
                    (parameter == 3 && x.rankList.rank.Contains(searchString)) ||
                    (parameter == 4 && x.eloList.rank.Contains(searchString)) ||
                    (parameter == 5 && x.roleList.lane.Contains(searchString)) ||
                    (parameter == 5 && x.roleList1.lane.Contains(searchString)) ||
                    (parameter == 6 && x.championList.Name.Contains(searchString)) ||
                    (parameter == 6 && x.championList1.Name.Contains(searchString)) ||
                    (parameter == 6 && x.championList2.Name.Contains(searchString)) ||
                    (parameter == 7 && x.summoner.Contains(searchString))
                    ).Distinct().ToList().OrderBy(x => x.name));
        }

        else
        // SORT STATEMENT
        {

            var Sorter = 1;
            try
            {
                Sorter = Int32.Parse(Sort);
            }
            catch
            {

            }
            switch (Sorter)
            {
                case 1:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                               .Distinct().ToList().OrderBy(x => x.name));
                case 2:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderByDescending(x => x.name));
                case 3:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderBy(x => x.summoner));
                case 4:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderByDescending(x => x.summoner));
                case 5:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderBy(x => x.school));
                case 6:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderByDescending(x => x.school));
                case 7:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderByDescending(x => x.eloList.ID));
                case 8:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderBy(x => x.eloList.ID));
                case 9:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderByDescending(x => x.rankList.ID));
                case 10:
                    return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                           .Distinct().ToList().OrderBy(x => x.rankList.ID));
            }

            return View(db.Users.Include(u => u.championList).Include(u => u.championList1).Include(u => u.championList2).Include(u => u.eloList).Include(u => u.rankList).Include(u => u.roleList).Include(u => u.roleList1)
                .Distinct().ToList().OrderBy(x => x.name));
        }
}

查看包含排序和过滤功能的代码段。

<tr>
        @using (Html.BeginForm("Index", "Users"))
        {
            <th class="col-xs-2">
                <h4 style="font-size: x-large; text-align: left">
                    <span style="font-weight: bolder; margin: 0px; padding: 0px">
                        Name
                        <button type="submit" class="glyphicon glyphicon-arrow-up btn btn-sm" value="1" name="Sort" id="sortNameUp" onclick="sortFunction(this, @Model)"></button>
                        <button type="submit" class="glyphicon glyphicon-arrow-down btn btn-sm" value="2" name="Sort" id="sortNameDown"></button>
                    </span>
                </h4>
            </th>
            <th class="col-xs-2">
                <h4 style="font-size: x-large; text-align: left">
                    <span style="font-weight: bolder; margin: 0px; padding: 0px">
                        IGN
                        <button type="submit" class="glyphicon glyphicon-arrow-up btn btn-sm" value="3" name="Sort" id="sortSummonerUp"></button>
                        <button type="submit" class="glyphicon glyphicon-arrow-down btn btn-sm" value="4" name="Sort" id="sortSummonerDown"></button>
                    </span>
                </h4>
            </th>
            <th class="col-xs-1">
                <h4 style="font-size:x-large; text-align:left">
                    <span style="font-weight:bolder; margin:0px; padding:0px">
                        School
                        <button type="submit" class="glyphicon glyphicon-arrow-up btn btn-sm" value="5" name="Sort" id="sortSchoolUp"></button>
                        <button type="submit" class="glyphicon glyphicon-arrow-down btn btn-sm" value="6" name="Sort" id="sortSchoolDown"></button>
                    </span>
                </h4>
            </th>

            <th class="col-xs-2">
                <h4 style="font-size: x-large; text-align: left">
                    <span style="font-weight: bolder; margin: 0px; padding: 0px">
                        Tier
                        <button type="submit" class="glyphicon glyphicon-arrow-up btn btn-sm" value="7" name="Sort" id="sortTierUp"></button>
                        <button type="submit" class="glyphicon glyphicon-arrow-down btn btn-sm" value="8" name="Sort" id="sortTierDown"></button>
                    </span>
                </h4>
            </th>
            <th class="col-xs-1">
                <h4 style="font-size:x-large; text-align:left">
                    <span style="font-weight: bolder; margin: 0px; padding: 0px">
                        Group

                        <button type="submit" class="glyphicon glyphicon-arrow-up btn btn-sm btn-group" value="9" name="Sort" id="sortMemberUp"></button>
                        <button type="submit" class="glyphicon glyphicon-arrow-down btn btn-sm btn-group" value="10" name="Sort" id="sortMemberDown"></button>

                    </span>
                </h4>
            </th>
        }
        <th class="col-xs-3">
            @using (Html.BeginForm("Index", "Users"))
            {
                <div class="dropdown">
                    <select class="btn btn-group-lg btn-default col-xs-5" style="margin-top: 15px; padding:0px; height: 36px; opacity: 1" data-toggle="dropdown" name="Filter">
                        <option value="0" disabled selected>Filter By:</option>
                        <option value="1">Name</option>
                        <option value="7">Summoner</option>
                        <option value="2">School</option>
                        <option value="3">Standing</option>
                        <option value="4">Tier</option>
                        <option value="5">Role</option>
                        <option value="6">Champion</option>
                    </select>
                </div>

                <input type="text" name="searchString" class="col-xs-5" style="margin-top: 16px; text-align:center; height:35px; font-size:20px" placeholder="enter text" />
                <button type="submit" class="btn btn-group-lg btn-primary col-xs-2 glyphicon glyphicon-arrow-right" style="margin-top: 15px; height:36px; opacity:1; padding:0px" value="" />
            }
        </th>
    </tr>

我很肯定有办法做一些POST调用来获取当前View的数据/模型并按它排序,但我找不到任何不使用插件的东西。有人可以协助我在我的索引控制器中创建一个排序功能,这将允许我预先形成OrderBy当前数据吗?

非常感谢!

0 个答案:

没有答案