MVC_Future_Guru
MVC_Future_Guru

Reputation: 239

Search function failing me asp.net mvc

I'm trying to add a simple search box. I don't receive any errors, my variable is populating correctly, but it's not updating the query. Here is my controller.

    public ActionResult Index(string sortOrder, string searchString)
    {
        ViewBag.LastNameSortParm = String.IsNullOrEmpty(sortOrder) ? "lastname_desc" : "";
        ViewBag.FirstNameSortParm = sortOrder == "firstname" ? "firstname_desc" : "firstname";
        var users = from u in UserManager.Users
                       select u;
        if (!String.IsNullOrEmpty(searchString))
        {
            users = UserManager.Users.Where(u => u.LastName.Contains(searchString));
                                   //|| u.FirstName.Contains(searchString));
        }
        switch (sortOrder)
        {
            case "lastname_desc":
                users = UserManager.Users.OrderByDescending(u => u.LastName);
                break;
            case "firstname":
                users = UserManager.Users.OrderBy(u => u.LastName);
                break;
            case "firstname_desc":
                users = UserManager.Users.OrderByDescending(u => u.LastName);
                break;
            default:
                users = UserManager.Users.OrderBy(u => u.LastName);
                break;
        }
        return View(users.ToList());

    }

And I have this within my view.

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

I have a LastName field within the applicationuser model, if I have a last name of Smith and I type in S, searchString populates with "S" while debugging, but the view is not updated. Am I missing something obvious?

Upvotes: 0

Views: 32

Answers (1)

infiniteRefactor
infiniteRefactor

Reputation: 2120

Last switch statement that is trying to handle sort order sets user to new values at each branch. All the upper assignments to the user are lost, including that assignment which filters your users:

users = UserManager.Users.Where(u => u.LastName.Contains(searchString));

You should use UserManager.Users only once. Since we don't see sort order stuff, or your object types completely I can not provide an exact solution but something like this might work:

public ActionResult Index(string sortOrder, string searchString)
{
    ViewBag.LastNameSortParm = String.IsNullOrEmpty(sortOrder) ? "lastname_desc" : "";
    ViewBag.FirstNameSortParm = sortOrder == "firstname" ? "firstname_desc" : "firstname";

    var users = from u in UserManager.Users
                   select u;

    if (!String.IsNullOrEmpty(searchString))
    {
        users = users.Where(u => u.LastName.Contains(searchString));                                   
    }

    switch (sortOrder)
    {
        case "lastname_desc":
            users = users.OrderByDescending(u => u.LastName);
            break;
        case "firstname":
            users = users.OrderBy(u => u.LastName);
            break;
        case "firstname_desc":
            users = users.OrderByDescending(u => u.LastName);
            break;
        default:
            users = users.OrderBy(u => u.LastName);
            break;
    }
    return View(users.ToList());

}

There is a possibility that type mismatches might occur, then you can define separate IEnumerable or List type variables at each step instead of rewriting users each time.

Upvotes: 2

Related Questions