Search code examples
c#asp.net.netasp.net-mvcrazor

ASP.NET Core redirecting to the same page after submitting a form with HttpPost method


I am developing a web application using ASP.NET Core 2.0. First of all i have a main view called NewOrder.cshtml:

@using Microsoft.AspNetCore.Mvc.Rendering
@using MokaKukaMap.Orders.ViewModels.Models
@model MokaKukaMap.Orders.ViewModels.Models.NewOrderViewModel

@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Html.Partial("_NewOrder")

In this main view I use a form in a partial view (_NewOrder.cshtml):

<form asp-controller="NewOrder" asp-action="CreateOrder" class="well">
    <div id="form-horizontal">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <input type="hidden" id="eventID" class="form-control">
        <div class="form-group">
            <label asp-for="Address" class=""></label>
            <div class="">
                <input asp-for="Address" class="form-control">
                <span asp-validation-for="Address" class="text-danger"> </span>
            </div>
        </div>
        <div class="form-group">
            <label asp-for="PhoneNumber" class=""></label>
            <div class="">
                <input asp-for="PhoneNumber" class="form-control">
                <span asp-validation-for="PhoneNumber" class="text-danger"> </span>
            </div>
        </div>
    </div>
    <div id="ErrorsBox" class="alert alert-danger" style="display: none;"></div>
    <div class="form-group">
        <div class="">
            <input type="submit" formnovalidate="formnovalidate" value="Submit new order" class="btn btn-primary" />
        </div>
    </div>
</form> 

And of course I have the corresponding controller and HttpPost action method, which is the following:

public class NewOrderController : Controller
{
    private readonly INewOrderSaverRequestHandler _newOrderSaverRequestHandler;

    public NewOrderController(INewOrderSaverRequestHandler newOrderSaverRequestHandler)
    {
        _newOrderSaverRequestHandler = newOrderSaverRequestHandler;
    }

    public IActionResult NewOrder()
    {
        return View();
    }

    [HttpPost]
    public IActionResult CreateNewOrder(NewOrderViewModel newOrder)
    {
        _newOrderSaverRequestHandler.Handle(newOrder, ModelState);

        return RedirectToPage("NewOrder");
    }
}

The issue is that if I click on the submit button, the page is redirected to the following url: "http://localhost:13078/NewOrder/CreateOrder". But what I would like is to stay on the same page, so at "http://localhost:13078/NewOrder/NewOrder" url

I have already tried some solutions which I've found here at stackoverflow:

  • Using return RedirectPage("NewOrder") - with this I am still redirected to .../NewOrder/CreateOrder so to an 404 error page. Furthermore I tried to use it with other cobinations like return RedirectPage("/") or return RedirectPage("NewOrder/NewOrder") but none of them led to the correct solution
  • With return View("NewOrder") i am still directed to ".../NewOrder/CreateOrde" as well
  • I also tried the following: return Page() but this dependency/reference is unknown in asp.net core 2.0.
  • Furthermore i tried to solve my issue with setting the "asp-route-returnurl" attribute in my html form tag, but no success with it.

Well, I think that maybe my issue has something to do with my routing. I use the following routing-setting in my Startup.cs:

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Map}/{action=Index}/{id?}");
    });

Last but not least I tried to use annotation routing, but it could also not solve my issue:

    [Route("[controller]/NewOrder")]
    [HttpPost]
    public IActionResult CreateNewOrder(NewOrderViewModel newOrder)
    {
        _newOrderSaverRequestHandler.Handle(newOrder, ModelState);

        return RedirectToPage("NewOrder");
    }

So what I want is that after clicking the submit button, create a new order (which is handled by INewOrderSaverRequestHandler) then stay on the same page. What do you think, where the problem lies in my code? Thanks in advance!


Solution

  • please replace this

     <form asp-controller="NewOrder" asp-action="CreateOrder" class="well">
    

    with

       <form asp-controller="NewOrder" asp-action="NewOrder" class="well">
    

    you are using asp-action="CreateOrder" thats why it is going to CreateOrder method

    try this return RedirectToAction("ActionMethod", "Controller");