I have a form in C# ASP, and two buttons submit with different path.
<form asp-action="">
<button type="submit" data-action="Liquidation" >
<button type="submit" data-action="Return" >
I try to modify the path with Jquery like that :
$('.btn-method-switchable').click(function(e) {
e.preventDefault();
let value = $(this).data('action')
$('form').attr('asp-action', value);
$('form').submit();
})
I enter in my Controller but not on the method Liquidation or Return.
Thanks for your answers
CodePudding user response:
The asp-action
attribute is only used before rendering. When the page is rendered it's converted to a standard HTML form action
attribute.
Also note that on the client side its value is converted from the reference to the action name to a relative URL to the route. More detail on that in the documentation.
Therefore to correct your code you need to set the action
attribute and also amend data-action
on the button
elements to be the full route. This can be done using the Url.Action()
helper. The code would look something like this:
<form>
<button type="submit" data-action="@Url.Action("Liquidation")" >Foo</button>
<button type="submit" data-action="@Url.Action("Return")" >Bar</button>
</form>
$('.btn-method-switchable').click(function(e) {
e.preventDefault();
let value = $(this).data('action')
$('form').attr('action', value).submit();
})