1
votes

I've been trying to understand what is happening here with no luck. I have a controller with some actions inside of it, two of them marked as post via attributes. Those two posts are to map to two different forms in my view using the asp-route helper. The three routes(all of them are being registered using the Route attribute and an specific name) needs an integer parameter as part of the route which is being constrained to int. The problem arises when setting the second asp-route parameter, and the problem is that the generated action attribute in the form tag is not being filled.

Let me share some code to explain this better.

Controller:

[Route("teams")]
public class TeamsController: Controller
{
    [Route("list/{groupId:int}", Name = "TeamsList")]
    public IActionResult List(int groupId)
    {
        //some logic
        return View(viewmodel);
    }

    [Route("list/{groupId:int}", Name = "TeamListPost")]
    [HttpPost]
    public IActionResult List(int groupId, int activityId)
    {

        //some logic

        return RedirectToRoute("TeamsList", new {groupId});
    }

    [Route("details/{groupId:int}", Name = "TeamDetailsPost")]
    [HttpPost]
    public IActionResult Details(int groupId)
    {

        //some logic

        return RedirectToRoute("TeamsList", new {groupId});
    }
}

View:

<form asp-route="TeamListPost"> 
   <button type="submit">Send post</button> 
</form>
<form asp-route="TeamDetailsPost">
   <button type="submit">Send another post</button>
</form>

The first form generates the action attribute to be set to /teams/list/1 if visiting /teams/list/1 nothing wrong here, I would expect the second form to generate the action attribute set to /teams/details/1 if visiting /teams/list/1 but I got an empty action attribute instead.

One interesting thing I noticed is that when I remove the constraint from the TeamDetailsPost route it at least renders the action attribute as /team/details which makes no sense for me.

Has anyone any idea what would be happening?

1

1 Answers

0
votes

I could reproduce your issue when I display the two forms in Temas/List.cshtml.Your request for this List.cshtml is teams/list/1.So you could generated the url by default for the first form.But the default AnchorTagHelper could not find the id for the different action name.

To fix such issue,you need to specify the id manually in the second form and the id you could get from the request url:

<form asp-route="TeamDetailsPost" asp-route-groupId="@Context.Request.RouteValues["groupId"]">
    <button type="submit">Send another post</button>
</form>

Result: enter image description here