简体   繁体   中英

CRUD filtering/sorting doesnt work in asp.net

I have this database that I need to sort/filter. My current code shows no errors, but it does nothing as well.

ApplicationUsersController.cs

// GET: ApplicationUsers
        public ActionResult Index(string sortOrder, string searchString)
        {
            ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : "";
            ViewBag.EmailSortParm = sortOrder == "Email" ? "email_desc" : "Email";
            var users = from c in db.Users
                           select c;
            if (!String.IsNullOrEmpty(searchString))
            {
                users = users.Where(c => c.Name.ToUpper().Contains(searchString.ToUpper())
                                       || c.Email.ToUpper().Contains(searchString.ToUpper()));
            }
            switch (sortOrder)
            {
                case "name_desc":
                    users = users.OrderByDescending(c => c.Name);
                    break;
                case "Email":
                    users = users.OrderBy(c => c.Email);
                    break;
                case "email_desc":
                    users = users.OrderByDescending(c => c.Email);
                    break;
                default:
                    users = users.OrderBy(c => c.Name);
                    break;
            }




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

Index.cshtml

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



<table class="table">
    <tr>
        <th>
            @Html.ActionLink("Email", "Index", new { sortOrder = ViewBag.EmailSortParm })
        </th>
        <th>
            @Html.ActionLink("Name", "Index", new { sortOrder = ViewBag.NameSortParm })
        </th>

Neither sorting, nor filtering seems to work. When sorting, address bar changes to

/ApplicationUsers?sortOrder=name_desc

but the order doesn't change at all. Maybe someone can see where is the problem?

EDIT:

Can it be ' var users = from c in db.Users select c; ' that the letter 'c' is wrong? How can I know for sure?

Change:

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

To:

return View(users);

:-)

您需要将两个参数传递给操作Index

@Html.ActionLink("Email", "Index", new { sortOrder = ViewBag.EmailSortParm, searchString = null })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM