'is it possible to pass route value using Ajax form submit?
The question: how to pass route value to the controller along with the form using Ajax?
I normally submit form using Ajax like this
function AjaxFormSubmit(frm) {
var form = `#${frm}`;
$(form).submit(function (e) {
e.preventDefault();
var actionUrl = $(form).attr('action');
$.ajax({
type: "POST",
url: actionUrl,
processData: false,
contentType: false,
data: $(form).serialize(),
success: function (data) {
alert(data);
}
});
});
}
Usage :
<button onclick="AjaxFormSubmit('formName')" class="btn btn-primary">Create</button>
The form itself
<form id="formName" asp-action="Create" asp-route-id="somedata">
.... some fields
Then I get the form submitted successfully but I never get the route value asp-route-id
I recive it in the C# controller side like this
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(TheModel modelName, [FromQuery]string id)
{
......
return Json(id);
}
or ... [FromRoute]string id
none of them is working
I get an empty white page with the value null !!
finally, how to pass route value to the controller along with the form using Ajax?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
