Reputation: 23
I am new to MVC and was wondering why/how the following occurs. I have two action methods in my controller for method TEST, one for Get and the other for Post. Each contain a "sortby" parameter. When I call the Get method, "sortby" is set to "ABC". When the submit button is clicked and the Post method is called, the "sortby" parameter for the Post method has the value of "ABC". My question is why? No hidden field values, model does not contain "sortby" field.
public ActionResult Test(string sortby = "")
{
return View(myModel);
}
[HttpPost]
public ActionResult Test(modelType myModel, string sortby = "")
{
return View(myModel);
}
Upvotes: 2
Views: 639
Reputation: 239290
Nothing is shared between requests to different actions. The only thing that exists is what was included in the request (query string for a GET, post body for a POST, and the URL itself).
You haven't given any information about how your URLs are set up, but assuming that you had a route like /test/{sortby}
, when you postback to that URL, the sortby
param would be filled from the URL, just as it would be with a GET. Otherwise, it must be included in the post body for it to be populated, meaning you do have a hidden field somewhere in your form holding that value.
Upvotes: 3
Reputation: 1217
Are you calling your POST after the GET which is returning the view with myModel data (and likely the sortby params defined)? More information is needed to tell you why this is happening, I'm just guessing, but they shouldn't be sharing anything. Somehow you are telling both methods to use those params.
Upvotes: 0