1

I'm working on this project that currently has the follow method:

 [HttpPost]
 public ActionResult Service(string identifier)

This function is currently being used by a webpage form

 <form method="POST" action="/Connector/Service">
 <input type="hidden" id="identifier" name="identifier" value="@TempData["Identifier"]"/>

So Service is used when the user clicks on a button that submits the form on the webpage. The user is taken to this page from another action. I have to implement a feature where sometimes instead of taking the user to the webpage, the user goes directly to the Service method from the action.

I found this thread when searching: ASP.NET MVC: RedirectToAction with parameters to POST Action

But it seems like that is bad design and returning RedirectToAction with Service actually did not work for me.

return RedirectToAction("Service", new {identifier})

Upon more search it seems like I actually cannot make a post request from my controller. Asp.NET MVC : redirect to another controller with POST Action

Any ideas on what I could do here? I am fairly new to ASP.NET and have no idea what to do at this point. All help is appreciated, thanks.

Community
  • 1
  • 1
SW Williams
  • 559
  • 1
  • 5
  • 18
  • I agree it is bad design. `"where sometimes instead of taking the user to the webpage"` - What is **sometimes**? That's the missing piece here – Mark C. Dec 15 '16 at 20:09
  • 1
    the way you call redirect to action is sending a GET request. But your method is marked as POST only – Steve Dec 15 '16 at 20:27
  • 1
    http://stackoverflow.com/questions/129335/how-do-you-redirect-to-a-page-using-the-post-verb you should read the accepted answer and see how @Jasonbunting does – ergen Dec 15 '16 at 20:46
  • @MarkC. The webpage is a terms and conditions page. The user gets taken to the service from the terms and conditions page; I'm trying to have it so if the user already accepted the terms it'll take them directly to the service instead of the webpage. – SW Williams Dec 15 '16 at 22:36
  • I'm not sure why the person wrote it as POST only, but I'm assuming there was a reason, so I'm worried that it might potentially break more things if I just change it to GET. – SW Williams Dec 15 '16 at 22:38
  • What information does the form post exactly? – Mark C. Dec 15 '16 at 23:48
  • There's a hidden input that gives identifier a value from @TempData["Identifier"]. – SW Williams Dec 16 '16 at 00:50
  • , and then theres a submit button. I'm not sure why they're doing it this way – SW Williams Dec 16 '16 at 00:50
  • 3
    hey you can call the function like a normal one, like this ` Service(value)` – Anonymous Duck Dec 16 '16 at 02:52
  • @Sherlock hey that worked, thanks – SW Williams Dec 16 '16 at 17:38

2 Answers2

2

So turns out I could just call the function directly by doing

Service(identifier)

I kept thinking I had to use RedirectToAction so that didn't even cross my mind.

Thanks Sherlock for the answer!

Community
  • 1
  • 1
SW Williams
  • 559
  • 1
  • 5
  • 18
0

The tag [post] is used only when a form is submited by the view itself, if your page redirect to a controler function from another view than the one your in, you have two option:

  1. Use the [get] tag, you will probably have more luck with this tag, although i don't know exactly how it work (this might be edited)

  2. Use the tagless function with parameters, if you rediret from another view to a function with a parameter, like public ActionResult Service(string identifier) that has no tag, you will automatically reach this function.

    if the parameter haven't been specified, it will simply be NULL.

Community
  • 1
  • 1
Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62