简体   繁体   中英

Update content depending on string in view model

I am running an MVC application, which have a view model like this:

public class UserListViewModel
{
    public UserListViewModel()
    {
        Users = new List<UserViewModel>();
        QueriedUsers = new List<UserViewModel>();
    }

    public List<UserViewModel> Users { get; private set; }
    public string SearchQuery { get; set; }
    public List<UserViewModel> QueriedUsers { get; private set; }
}
  • Users : The original users
  • SearchQuery : a query which is returned by the view
  • QueriedUsers :Users after Users are sorted

In my view, I have the following code:

@using (Html.BeginForm("Search", "EditUser"))
{ 
    @Html.EditorFor(model => Model.SearchQuery);
}



@if (Model.QueriedUsers != null && Model.QueriedUsers.Any())
{
    foreach (var u in Model.QueriedUsers)
    {
        // do stuff
    }
}
else
{ 
    foreach (var u in Model.Users)
    {
        // do stuff
    }
}

And this is the HttpPost code in the controller:

[HttpPost]
public ActionResult Search(UserListViewModel input)
{
    return View("EditUser",NewUserListModel(input));
}

Before I click enter in my search field, I have a URL like this:

http://localhost:61162/Admin/Users

After clicking enter, I get:

http://localhost:61162/Admin/Users/Search

From my breakpoint I can tell the controller code is run.

The error I get in my new URL is:

The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Admin/EditUser/Search

Which of course is caused by the url has changed.

Any hints how to solve my issue ? :-) I simply want to have a where clause on my Users, so I can show the relevant data depending on the search input.

From @LarsHoldgaard's last comment:

Thanks a lot for your help, I got really confused with the project. However, the solution was changing the Search HttpPost to:

  return View("Index", NewUserListModel(input)); 

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