C
C#5mo ago
Alizer

asp core form tag helpers but the router has dynamic route

i have this form
<form id="chequeForm" asp-route-payrollHistoryId="@(payrollHistory?.Id)" asp-action="GenerateCheque" asp-controller="DocumentGenerations" method="post">
<form id="chequeForm" asp-route-payrollHistoryId="@(payrollHistory?.Id)" asp-action="GenerateCheque" asp-controller="DocumentGenerations" method="post">
now the endpoint it points to is below
c#
[ApiController]
[Route("/api/documents")]
[Authorize(Roles = $"{Roles.Administrator},{Roles.Authority}")]
public class DocumentGenerations : Controller
{
private readonly PayrollHistories _payrollHistories;
private readonly BankOrderGenerator _bankOrderGenerator;

public DocumentGenerations(PayrollHistories payrollHistories, BankOrderGenerator bankOrderGenerator)
{
_payrollHistories = payrollHistories;
_bankOrderGenerator = bankOrderGenerator;
}

// below is my target form i want to submit the form to
[HttpPost("{payrollHistoryId}/cheque")]
public async Task<IActionResult> GenerateCheque([FromForm] ChequeGeneration chequeGeneration,
[FromRoute] string? payrollHistoryId)
{
var payrollHistories = await _payrollHistories.GetPayrollHistoryByIdAsync(payrollHistoryId);

return Ok();
}
c#
[ApiController]
[Route("/api/documents")]
[Authorize(Roles = $"{Roles.Administrator},{Roles.Authority}")]
public class DocumentGenerations : Controller
{
private readonly PayrollHistories _payrollHistories;
private readonly BankOrderGenerator _bankOrderGenerator;

public DocumentGenerations(PayrollHistories payrollHistories, BankOrderGenerator bankOrderGenerator)
{
_payrollHistories = payrollHistories;
_bankOrderGenerator = bankOrderGenerator;
}

// below is my target form i want to submit the form to
[HttpPost("{payrollHistoryId}/cheque")]
public async Task<IActionResult> GenerateCheque([FromForm] ChequeGeneration chequeGeneration,
[FromRoute] string? payrollHistoryId)
{
var payrollHistories = await _payrollHistories.GetPayrollHistoryByIdAsync(payrollHistoryId);

return Ok();
}
but for some reason it points to the wrong route, the generated html is
<form id="chequeForm" method="post" action="/DocumentGenerations/GenerateCheque">
<form id="chequeForm" method="post" action="/DocumentGenerations/GenerateCheque">
as you can see it points to /DocumentGenerations/GenerateCheque which is wrong, but this is probably becaus of the {payrollHistoryId} in the route, how would i fix this?
4 Replies
Angius
Angius5mo ago
I don't think the form helper takes asp-route-<param> attribute, it's only for links I'd rather send the payrollHistoryId with the form, as a hidden field perhaps To a non-dynamic route
Alizer
AlizerOP5mo ago
yeah i figured alright i guess i just have to write the dynamic route
Alizer
AlizerOP5mo ago
No description
Angius
Angius5mo ago
Or, again, don't
Want results from more Discord servers?
Add your server