Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 我的排序在razor视图中不起作用,但在我的代码中,我得到了正确的结果_Asp.net Mvc_Sorting_Razor - Fatal编程技术网

Asp.net mvc 我的排序在razor视图中不起作用,但在我的代码中,我得到了正确的结果

Asp.net mvc 我的排序在razor视图中不起作用,但在我的代码中,我得到了正确的结果,asp.net-mvc,sorting,razor,Asp.net Mvc,Sorting,Razor,我有一个应用程序,其中我有departments视图department表有Id、name、Active和datecreated列,我想根据名称和日期进行排序,所以我在我的index controller中编写了以下代码 我已经尝试传递查询参数,它工作正常,我的查询生成100%正确,但在视图中显示的结果不正确 public ActionResult Index(string sortOrder) { ViewBag.NameSortParm = Stri

我有一个应用程序,其中我有departments视图department表有Id、name、Active和datecreated列,我想根据名称和日期进行排序,所以我在我的index controller中编写了以下代码

我已经尝试传递查询参数,它工作正常,我的查询生成100%正确,但在视图中显示的结果不正确

 public ActionResult Index(string sortOrder)
        {
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";
            var dept = from s in db.Departments
                           select s;
            switch (sortOrder)
            {
                case "name_desc":
                    dept = dept.OrderByDescending(s => s.Department_Title);
                    break;
                case "Date":
                    dept = dept.OrderBy(s => s.Date_Created);
                    break;
                case "date_desc":
                    dept = dept.OrderByDescending(s => s.Date_Created);
                    break;
                default:
                    dept = dept.OrderBy(s => s.Department_Title);
                    break;
            }
            return View(db.Departments.ToList());
        }
下面是我的观点

 <table class="table table-condensed">
        @*<tr>
            <th>
                @Html.ActionLink("Last Name", "Index", new { sortOrder = ViewBag.NameSortParm })
            </th>
            <th>
                First Name
            </th>
            <th>
                @Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
            </th>
            <th></th>
        </tr>*@



        <tr>
            <th>
                @Html.ActionLink("Department Name","Index",new {sortOrder=ViewBag.NamesortParm })
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Status)
            </th>
            <th>
                @Html.ActionLink("Creation Date","Index",new {sortOrder=ViewBag.DateSortParm })
            </th>
            @*<th>
                @Html.DisplayNameFor(model => model.Date_Modified)
            </th>*@
            <th>Edit/Delete</th>
        </tr>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Department_Title)
                </td>
                <td>
                    @if (item.Status != null)
                    {
                        @(item.Status.Value ? Html.Raw("ACTIVE") : Html.Raw("NOT ACTIVE"))
                    }
                    @*@Html.DisplayFor(modelItem => item.Status)*@
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Date_Created)
                </td>
                @*<td>
                    @Html.DisplayFor(modelItem => item.Date_Modified)
                </td>*@
                <td colspan="3">
                    @Html.ActionLink("Edit", "Edit", new { id = item.Department_Id }, new { onclick = "return confirm('Are you sure you wish to delete?');", @class = "btn btn-sm btn-info" })
                    @*@Html.ActionLink("Details", "Details", new { id=item.Department_Id }) |*@
                    @Html.ActionLink("Delete", "Delete", new { id = item.Department_Id }, new { onclick = "return confirm('Are you sure you wish to delete?');", @class = "btn btn-sm btn-danger" })
                </td>
            </tr>
        }

    </table>

它应该在视图中对结果进行排序~

很抱歉,我没有返回过滤列表,而是返回了返回视图(db.Departments.ToList());但它应该是返回视图(dept.ToList())

您必须返回var dept变量,因为它已经包含您在交换机案例中的排序列表。db.department将返回插入数据库中的实际部门列表。因此,使用dept.toList()代替db.database.toList()

SELECT 
    [Extent1].[Department_Id] AS [Department_Id], 
    [Extent1].[Department_Title] AS [Department_Title], 
    [Extent1].[Status] AS [Status], 
    [Extent1].[Date_Created] AS [Date_Created], 
    [Extent1].[Date_Modified] AS [Date_Modified]
    FROM [dbo].[Departments] AS [Extent1]
    ORDER BY [Extent1].[Department_Title] DESC