1

i am trying to redirect to another action within the same controller action is called index

[HttpGet]
public ActionResult Search(string city)
{
    return RedirectToAction("Index", "Rentals", new { CityName = city });

}

this is index action

[HttpPost]
public ActionResult Index(String CityName)
{


}

am i missing something?

tereško
  • 58,060
  • 25
  • 98
  • 150
kayze
  • 738
  • 8
  • 19
  • 33
  • What happens if you change the attribute from HttpPost to HttpGet ? – Scott Prokopetz Jul 25 '14 at 04:14
  • if you are looking some kind of hack like [here](https://stackoverflow.com/questions/16643414/asp-net-mvc-redirecttoaction-with-parameters-to-post-action) – Mantra Jan 24 '20 at 17:28

3 Answers3

3

You are trying to redirect action which is searching for a matching action but in this case there is no get action, so you have to add a get method to accept redirect. If you want, you can check the HTTPGET or POST inside the method

[HttpPost]<---- Remove this 
public ActionResult Index(String CityName)
{


}
Chamika Sandamal
  • 23,565
  • 5
  • 63
  • 86
0

Please change HttpPost to HttpGet

[HttpGet]
public ActionResult Index(String CityName)
{


}

Because whenever you call the Action, then the GET method will be first called.

-1

As the two actions are in the same controller, you could call the Index method directly from Search like this:

return Index(city);

not necessarily to user the RedirectToAction method.

ssett
  • 432
  • 3
  • 6