0

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?

2 Answers 2

2

Change:

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

To:

return View(users);

:-)

Sign up to request clarification or add additional context in comments.

1 Comment

I can't believe how simple that was. Thank you.
1

you need to pass two params to the action Index:

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

2 Comments

Thanks for the help, but after putting this line I'm getting Cannot assign <null> to anonymous type property After putting "" instead of "null", sorting still doesn't work.
Can it be 'var users = from c in db.Users select c; ' that the letter 'c' is wrong? How can I know for sure?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.