Return partial view to the same view

I have a view, which consists of 3 partial views of 2 parts at the top left and right of the screen. Each of them has a mutually exclusive search, and each answer returns to its own action.

This is an action that displays the initial view.

public ActionResult Index() { var model = new SearchOptionsViewModel(); return View(model); } 

This message is for one of the partial views:

  [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 publishes and receives my data from the database and adds it to the viewmodel. I have lost information on how to show results on one screen under the search criteria.

This action, which I was hoping for, will work to get the model from the previous action and make a partial inverse to the original form. I thought that using the third search query, SearchResults, was the answer, but that didn’t work, it translates to its own page, showing the results, and not on one page.

  public ActionResult SearchResults(UserSearchResultsListViewModel model) { return PartialView(model); } 

A model always comes as null when I do this. I am missing something fundamental here ... How do I get this data to display on the same view?

+4
source share
1 answer

This is what I ended up doing. It does not feel completely clean, but I kept the responsibilities of these methods minimal. I would like to have a better way to do this without TempData, if anyone has any better suggestions, 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; } 
+1
source

Source: https://habr.com/ru/post/1394075/


All Articles