排序和搜索表中的数据不起作用

时间:2015-04-22 17:14:24

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

我按照以下教程进行排序和搜索:

http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application

当我尝试这样做时,排序不起作用。我希望对3个字段进行排序,用户名 user_role date_of_register 。我的显示的数据没有按任何顺序排序(仅显示数据)。 请任何人帮助我,我做错了什么。

//controller code:
    public ActionResult DisplayUsers(string sortOrder, string searchString)
        {
            ViewBag.NameSortParm1 = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.NameSortParm2 = String.IsNullOrEmpty(sortOrder) ? "role_desc" : "";
            ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

            var users = from a in db.app_user
                            select a;

            switch (sortOrder)
            {
                case "name_desc":
                    users = users.OrderByDescending(a => a.username);
                    break;
                case "role_desc":
                    users = users.OrderBy(a => a.user_role);
                    break;
                case "date_desc":
                    users = users.OrderBy(a => a.date_of_register);
                    break;
                default:
                    users = users.OrderBy(a => a.username);
                    break;
            }

            return View(db.app_user.ToList());
        } 

查看页面

//DisplayUsers.cshtml page
    <table class="table">
        <thead>
            <tr>
                <th>
                    @Html.ActionLink("User Name", "DisplayUsers", new { sortOrder = ViewBag.NameSortParm1 })
                </th>
                <th>
                    @Html.ActionLink("User Role", "DisplayUsers", new { sortOrder = ViewBag.NameSortParm2 })
                </th>
                <th>
                    @Html.DisplayNameFor(model => model.is_active)                
                </th>
                <th>
                    @Html.ActionLink("Enrollment Date", "DisplayUsers", new { sortOrder = ViewBag.DateSortParm })
                </th>
                <th></th>
            </tr>
        </thead>

        @foreach (var item in Model)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.username)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.user_role)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.is_active)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.date_of_register)
                </td>            
            </tr>
        }
    </table>

当我这样做时:return View(users.ToList()); 排序仅基于字段用户名,但不包括用户角色和注册日期。我无法根据用户角色或注册日期对数据进行排序为什么会这样?

搜索: 我尝试在用户名和date_of_register字段上进行搜索。当我尝试代码时:

if (!String.IsNullOrEmpty(searchString))
    {
        users = users.Where(a => a.username.Contains(searchString) || a.date_of_register.Contains(searchString));
    }

得到错误:

  

错误1实例参数:无法转换为&#39; System.DateTime?&#39;至   &#39; System.Linq.IQueryable&#39;

     

错误2&#39; System.DateTime?&#39;不包含的定义   &#39;包含&#39;和最好的扩展方法重载   &#39; System.Linq.Queryable.Contains(System.Linq.IQueryable,   TSource)&#39;有一些无效的论点

如何在日期字段中进行搜索? 请帮助我,对此有任何帮助将非常感谢。

2 个答案:

答案 0 :(得分:0)

您没有返回users变量。修正了

public ActionResult DisplayUsers(string sortOrder, string searchString)
{
    ViewBag.NameSortParm1 = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
    ViewBag.NameSortParm2 = String.IsNullOrEmpty(sortOrder) ? "role_desc" : "";
    ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date";

    var users = from a in db.app_user
                select a;

    switch (sortOrder)
    {
        case "name_desc":
            users = users.OrderByDescending(a => a.username);
            break;
        case "role_desc":
            users = users.OrderBy(a => a.user_role);
            break;
        case "date_desc":
            users = users.OrderBy(a => a.date_of_register);
            break;
        default:
            users = users.OrderBy(a => a.username);
            break;
    }

    return this.View(users.ToList());
    // return View(db.app_user.ToList());
} 

关于你问题的第2部分......

date_of_register的类型为DateTime?,可以为空的DateTime类型不包含名为Contains的方法,对它来说也没有意义...如果你想检查是否该字符串的类型为DateTime,您可以尝试使用以下代码解析它...

if (!String.IsNullOrWhiteSpace(searchString))
{
    DateTime dateOfRegistration;

    bool isDateTime = DateTime.TryParse(searchString, out dateOfRegistration);
    if (isDateTime)
    {   
        users = users.Where(user => user.date_of_register == dateOfRegistration);
    }
    else 
    {
        users = users.Where(a => a.username.Contains(searchString));
    }
}

答案 1 :(得分:0)

  

排序:

问题是您使用预取列表,但是您没有将其发送到查看。

return View(db.app_user.ToList()); 

应该是

return View(user.ToList());
  

搜索:

您再次使用Contains,期望该对象可查询而datetime不是,因此您需要为datetime过滤器编写一个特殊情况:

DateTime datetime = Convert.ToDateTime(searchString);
if (datetime.TimeOfDay != TimeSpan.Zero) //comparison with 00:00:00 time
{
    filterresults = filterresults.Where(x => x.TimeStarted == datetime);
}
else
{
    DateTime plusOneDateTime = datetime.AddDays(1).Date;
    filterresults = filterresults.Where(x => x.TimeStarted >= datetime.Date 
                                           && x.TimeStarted < plusOneDateTime);
}