Reputation: 6607
I have a view that is comprised of 3 partial views 2 partials at the top on left and right of screen. Each one has a mutually exclusive search and each post back to their own action
This is the action that renders the initial view
public ActionResult Index() {
var model = new SearchOptionsViewModel();
return View(model);
}
This is the post action for one of the partial view:
[HttpPost]
public ActionResult SearchByUser(UserSearchOptionsViewModel model) {
if(ModelState.IsValid) {
var list = SearchUserService.SearchByValue(model.LastName, model.Username, model.EmailAddress);
if(list != null) {
var resultsModel = new UserSearchResultsListViewModel();
list.ForEach(item => resultsModel.Users.Add(new UserSearchResultsViewModel(item)));
return RedirectToAction("SearchResults", resultsModel);
}
}
return View(model);
}
This model successfully posts and gets my data from the db and adds it to the viewmodel. I'm lost on as to how to get the results to show on the same screen underneath the search criteria.
This is the action that I was hoping would work to receive the model from the prior action and render the partial back to the original view. I thought using a 3rd SearchResults partialview was the answer but that doesn't work, it navigates to it's own page showing the results instead of on the same page.
public ActionResult SearchResults(UserSearchResultsListViewModel model) {
return PartialView(model);
}
Model always comes in as null when I do this. I'm missing something fundamental here... How do I get this data to show on the same View?
Upvotes: 4
Views: 4487
Reputation: 6607
This is what I ended up doing. It doesn't feel completely clean but I've kept the responsibilities of the methods minimal. I'd like to have a better way of doing this without TempData if anyone has a better suggestions please post your example.
// Inital loaded view with 3 partial views
// 2 for search criteria, 1 for results
public ActionResult Index() {
var model = new SearchUsersViewModel();
if(TempData["model"] != null)
model = (SearchUsersViewModel)TempData["model"];
return View(model);
}
// Post the search criteria model for search
// by user data (by last name, etc...)
// Then redirect back to Index.
// TempData will have the full model with results populated
[HttpPost]
public ActionResult SearchByUser(FilterUsersByUserDataViewModel model) {
if(ModelState.IsValid) {
var list = SearchUserService.SearchByValue(model.LastName,
model.Username,
model.EmailAddress);
if(list != null) {
TempData["model"] = PrepareResultsModel(list);
return RedirectToAction("Index");
}
}
return View(model);
}
// This method just separates the concern of creating
//the new full model with search results populating the results view
private SearchUsersViewModel PrepareResultsModel(List<SearchUserViewDTO> list) {
var searchResults = new UserSearchResultsViewModel();
list.ForEach(item => searchResults.Users.Add(new UserViewModel(item)));
var model = new SearchUsersViewModel();
model.UserSearchResultsViewModel = searchResults;
return model;
}
Upvotes: 1