0

below is the controller:

public class HomeController : Controller
{
   public ViewResult Index() => View("SimpleForm");
   public ViewResult ReceiveForm(string name, string city) => View("Result", $"{name} lives in {city}");
}

and the SimpleForm.cshtml is:

 <form method="post" asp-action="ReceiveForm">
        <div class="form-group">
            <label for="name">Name:</label>
            <input class="form-control" name="name" />
        </div>
        <div class="form-group">
            <label for="name">City:</label>
            <input class="form-control" name="city" />
        </div>
        <button class="btn btn-primary center-block" type="submit">Submit</button>
 </form>

so when I run the application, the default Index action is used, so then I fill up the form and click submit button to trigger a post request and direct to ReceiveForm action, which display the result in a view. So now the URL on my browser is /Home/ReceiveForm and I reloaded a browser, and realized a new post request is triggered, but how come it is post request, should it be a get request? isn't that reloading the browser is the same thing as copy /Home/ReceiveForm into the browser's address bar and press enter, which always trigger get requests?


  • A couple of things: your controller code as shown has only a single method `ReceiveForm()` but if you want to be able to POST / GET to the same route name you will need another method and probably an attribute indicating the HTTP verb. Once you have method / route available for GET you probably will also need to implement the pattern POST-REDIRECT-GET by having your POST method do the additional step of calling the GET method with `return RedirectToAction("ReceiveForm", "Home")` -- this should allow the browser to behave as you expect on refresh, after posting. – David Tansey Jul 31 '19 at 03:21
  • Refer to this [post](https://stackoverflow.com/questions/2126747/how-to-supress-re-post-when-refreshing-a-page-asp-net-mvc) and the links to other posts that are referred to by it. – David Tansey Jul 31 '19 at 03:22
  • @DavidTansey so why it is a get request when I copy and paste `/Home/ReceiveForm` into address bar and press enter, but it is post request when I refresh page after a initial post request? isn't that http stateless, so it couldn't know that 'oh because the user's the previous request is put request, so this time even user just refresh the page, I'm still going to trigger a post request? –  Jul 31 '19 at 03:54

1 Answers1

0

You are posting the to the ReceiveForm action which is then returning the view. Instead do the followings:

  • Create a httpGet version of the ReceiveForm
  • Create a httpPost version of the ReceiveForm
  • Load the UI from the httpGet version action
  • Post to the httpPost version action
  • After the post do the processing and redirect to the HttpGet version to load the UI again. To send data (e.g result) from the Post to the Get action during redirection you can use TempData object.
Rahatur
  • 3,147
  • 3
  • 33
  • 49