简体   繁体   中英

Action Routing: Application gets confused between the GET and POST methods

Context:

The view constructed with the GET method takes at this moment two parameters: id and date . In this view there is a form which sends information back to the Controller thru the POST method using the parameter id

GET Method

    [HttpGet]
    public async Task<IActionResult> DetailsAdmin(int? id, [ModelBinder(typeof(PModelBinder))]DateTime? date)
    {
       {...}
    }

POST Method

    [HttpPost, ActionName("DetailsAdmin")]
    [ValidateAntiForgeryToken]
    [Route("HechosLiquidadors/DetailsAdmin/{id}")]
    public async Task<IActionResult> DetailsAdmin(int? id)
    {
       {...}
    }

The problem:

When the form inside the View sends the information to the Controller, it goes to the GET Action instead of the POST action.

The form:

<form id="@(String.Format("{0}{1}","form",Model[i].HechosID))" 
asp-action="DetailsAdmin" method="post" asp-route-id="@Model[i].HechosID" ></form>

I've tried using a Custom Routing to the POST Action method but no luck. How can I correct this so the form points to the POST Action correctly?

In both action methods all parameters as optional. therefore Mvc cannot select "Best candidate" method for executing. change first action as below and check id not 0:

[HttpGet]
public async Task<IActionResult> DetailsAdmin(int id = 0, [ModelBinder(typeof(PModelBinder))]DateTime? date)
{
    if (id != 0)
    {

    }
    {...}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM