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.

10
  • 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 Commented Dec 15, 2016 at 20:09
  • 1
    the way you call redirect to action is sending a GET request. But your method is marked as POST only Commented Dec 15, 2016 at 20:27
  • 1
    stackoverflow.com/questions/129335/… you should read the accepted answer and see how @Jasonbunting does Commented Dec 15, 2016 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. Commented Dec 15, 2016 at 22:36
  • 3
    hey you can call the function like a normal one, like this ` Service(value)` Commented Dec 16, 2016 at 2:52

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

For #2, if I take away the Post tag will the view still be able to submit it?
@SWWilliams No, if you remove the POST you cannot submit data unless you change your form method to a GET. GET is used to retrieve remote data while POST is used to insert/update remote data.
Is it possible to allow the function to handle both POST and GET requests then?
No.. you can always make a separate function and call that function in post AND get

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.