【问题标题】:Table Sorting/Filtering with ASP.NET MVC 5 and Entity Framework使用 ASP.NET MVC 5 和实体框架进行表排序/过滤
【发布时间】:2018-09-15 08:32:48
【问题描述】:

我一直在关注 Microsoft 的 Contoso 教程,并构建了我的 Scaffolding 从异地 SQL Server 中提取数据表。我尝试插入与教程中的代码类似的代码,它会创建链接,但不会按应有的方式进行排序或过滤。它只刷新页面。某些东西似乎在起作用,因为当我点击链接进行排序时,URL 参数发生了变化,但表格没有改变。

这是排序/过滤操作的控制器

public ActionResult Index(string sortOrder, string searchString)
    {
        ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "Name_desc" 
: "";
        ViewBag.DateSortParm = sortOrder == "Date" ? "Date_desc" : "Date";
        var schedules = from s in db.schedules
                       select s;
        if (!String.IsNullOrEmpty(searchString))
        {
            schedules = schedules.Where(s =>s.designation.Contains(searchString)
                                   || s.Function.Contains(searchString));
        }
        switch (sortOrder)
        {
            case "Name_desc":
                schedules = schedules.OrderByDescending(s => s.designation);
                break;
            case "Date":
                schedules = schedules.OrderBy(s => s.promise_ship_date);
                break;
            case "Date_desc":
                schedules = schedules.OrderByDescending(s => s.promise_ship_date);
                break;
            default:
                schedules = schedules.OrderBy(s => s.promise_ship_date);
                break;
        }
        return View(db.schedules.ToList());
    }

这是我的索引页

@model IEnumerable<WebApplication1.Models.schedule>

@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
@Html.ActionLink("Create New", "Create")
</p>

@using (Html.BeginForm())
{
<p>
    Find by designation or function: @Html.TextBox("searchString")
    <input type="submit" value="Search"/>
</p>
}

<table class="table">
<tr>
    <th>
        @Html.ActionLink("Designation", "Index", new { sortOrder = ViewBag.NameSortParm })
    </th>
    <th>
        @Html.DisplayNameFor(model => model.Function)
    </th>
    <th>
        @Html.DisplayNameFor(model => model.lv_mv)
    </th>
    <th>
        @Html.ActionLink("Promise Ship Date", "Index", new { sortOrder = ViewBag.DateSortParm })

任何帮助将不胜感激。

【问题讨论】:

  • 为什么不调试代码看看到底发生了什么? VS 2017 不是免费的吗?

标签: asp.net-mvc entity-framework sorting filter


【解决方案1】:

您正在对名为 schedules 的集合进行排序,但将未排序的时间表直接从数据库返回到您的视图。试试return View(schedules)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-03
    • 2010-09-25
    • 1970-01-01
    • 2011-02-20
    • 1970-01-01
    相关资源
    最近更新 更多