3

i am teaching myself MVC and am struggling to work out the best solution to my problem. I have a search controller with a large amount of input fields. I will also have multiple overloads of the search fields eg basic search advanced search searchByCategory etc.

When the search form is posted i redirect to another action that displays the search results. If i press f5 the get action is fired again as opposed to the search results being refreshed in the action that my post redirects to. Ideally i would like to redirect to a search results Action Method without using the query string, or detect when refresh is hit and requery the database and just use different actions within the same search controller. I have read a lot of posts about this and the only 2 solutions i can find is using a session variable or TempData.Can anybody advise as to what is the best practice

4

3 回答 3

5

From the Comments Most of the time I prefer to use TempData in place of QueryString. This keeps the Url clean.

Question

Can anybody advise as to what is the best practice

Answer

Once the data is sent to Action Method to get the results from Database after then As per my knowledge you can use TempData to store the posted data. It is like a DataReader Class, once read, Data will be lost. So that stored data in TempData will become null.

var Value = TempData["keyName"] //Once read, data will be lost

So to persist the data even after the data is read you can Alive it like below

var Value = TempData["keyName"];
TempData.Keep();                   //Data will not be lost for all Keys
TempData.Keep("keyName");          //Data will not be lost for this Key

TempData works in new Tabs/Windows also, like Session variable does.

You could use Session Variable also, Only major problem is that Session Variable are very heavy comparing with TempData. Finally you are able to keep the data across Controllers/Area also.

Hope this post will help you alot.

于 2013-06-18T23:04:07.490 回答
0

I think there is no need to even call Get Method after performing search although its good habit in case of if your are performing any add/update/delete operation in database. But in your case you can just return the View from your post method and no need to store data in tempdata or session until you really don't need them again. So do something like this:

    [HttpPost]
    public virtual ActionResult PerformSearch(SearchModel model)
    {
         // Your code to perform search
          return View(model);
    }

Hope this will help.

于 2013-06-19T11:29:34.050 回答
0

Hi thanks

I have had a chance to revisit this. the problem was i neglected to mention that i am using jQuery mobile which uses Ajax by default even for a normal Html.BeginForm. I was also returning a view which i have since learned will not updated the URL but only render new html for the current controller. my solution is to set the action, controller and html attributes in the Html.Beginformas follows :

@Html.BeginForm("Index", "SearchResults", FormMethod.Post, new { data_ajax = "false" })

inside the parameters for the index action of the searchResults controller I have a viewModel that represents the fieldset of the form that i am posting. The data-ajax="false" disables the Ajax on the form post and MVC takes care of matching the form post parameters to my model. This allows the url to update and when i press f5 to refresh the controller re-queries the database and updates the search results.

Thanks everybody for your help. I was aware of TempData but it is good to know that this is preferred over session data so i voted up your answer

于 2013-06-25T22:33:30.107 回答