Eli
Eli

Reputation: 567

How to reload page with previously entered parameters?

I am working on a code project in Asp.net MVC. I have an issue with redirecting the user after they have completed an action. I have these controllers:

Index Search Page:

public ActionResult Index(){

    //this method sets up viewmodel data for search preferences

    Viewmodel obj = new Viewmodel();
    //set values of dropdowns and searching capabilities
    return View("Search", obj);
}

The user then fills out the search boxes in the view, chooses dropdowns. This will return a post search method that handles the data:

[HttpPost]
public ActionResult Index(Viewmodel obj, int? page)
{
    data = from i in db.Database
           select i;

    if(!String.IsNullOrEmpty(obj.Example)
    {
        data = data.Where(x => x.poss == obj.poss);
    }

    //PAGING and other data formatting here

    return View("Results", data);
}

Once the result list is displayed, I have a checkbox/button system in the result view that allows the user to select multiple results and mark them as "Good", "Bad" ETC. This is a method that changes the database very simply. My problem is that after the database alters the data, im not sure how to return the user back to the exact result set they were at. A method that returns void doesn't work, and the parameters are not separated, (one whole viewmodel), so i can't simply save the URL and return them back to the unique URL. I want to keep the viewmodel as the parameter. How can I save the viewmodel data that contains their search preferences for use later as well as the page number without changing my method to this:

[HttpPost]
public ActionResult Index(string dropdown1, string dropdown2, int num......){}

Upvotes: 2

Views: 1887

Answers (2)

kodebot
kodebot

Reputation: 1778

You can use TempData to achieve this.

Store ViewModel and Page in TempData within your POST Index action. The action method used to accept and store result status (i.e. good, bad, etc...) in the database will be able to access ViewModel and Page from the TempData given that it is the next immediate request. Once the database operation is done, just use RedirectToAction with the ViewModel and Page present in the TempData.

If the action which updates result status is not the next immediate request then you need to keep the data in session as answered by BGStack.

Upvotes: 1

BGStack
BGStack

Reputation: 1144

One thing I did was save view model to the session. Then I deserialize in the index method. Something like this in the index method:

[HttpGet]
public ActionResult AdvancedSearch()
{

        HttpContext currentContext = System.Web.HttpContext.Current;
        AdvancedSearchViewModel advancedSearchViewModel = (AdvancedSearchViewModel)Session["AdvancedSearchViewModel"];
        if (advancedSearchViewModel == null)
        {
            advancedSearchViewModel = new AdvancedSearchViewModel();
            AddAdvancedSearchLists(advancedSearchViewModel, currentContext);
        }

        return View(advancedSearchViewModel);

    }

Here is some code to save to session in the post:

Session["AdvancedSearchViewModel"] = advancedSearchViewModel;

Note that if you have listboxes (drop down and multi select) you have to rebuild the listboxes in the post method and reselect the selections (for multi select). HTML is stateless. MVC does not send the listbox contents back to the server on the post, it only sends what was selected.

Upvotes: 1

Related Questions