简体   繁体   English

动作路由:应用程序在GET和POST方法之间感到困惑

[英]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 . 此时,使用GET方法构造的视图需要两个参数: iddate In this view there is a form which sends information back to the Controller thru the POST method using the parameter id 在此视图中,存在一个表单,该表单使用参数id通过POST方法将信息发送回Controller

GET Method GET方法

    [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. 当视图内的表单将信息发送到控制器时,它将进入GET操作而不是POST操作。

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. 我试过使用自定义路由到POST操作方法,但是没有运气。 How can I correct this so the form points to the POST Action correctly? 我该如何纠正这个问题,以便表单正确指向POST操作?

In both action methods all parameters as optional. 在这两种操作方法中,所有参数都是可选的。 therefore Mvc cannot select "Best candidate" method for executing. 因此,Mvc无法选择“最佳候选”方法来执行。 change first action as below and check id not 0: 更改如下第一个操作,并检查ID不为0:

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

    }
    {...}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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