Reputation: 355
I have successfully created a product but the page doesn't redirects to the view I want...and I don't really know why... [HttpPost] public async Task AddProduct([FromForm] AddProductViewModel model)
The jquery call:
$.ajax({
url: '/' + 'Product' + '/' + 'AddProduct',
type: 'POST',
contentType: false,
processData: false,
data: formData,
success: console.log('success'),
});
//edit
$.ajax({
url: '/Product/AddProduct',
type: 'POST',
contentType: false,
processData: false,
data: formData,
success: window.location.href = "/Product/ProductList"
});
Upvotes: 0
Views: 51
Reputation: 2825
HTTP doesn't support redirection to a page using POST. When you redirect somewhere, the HTTP "Location" header tells the browser where to go, and the browser makes a GET request for that page. You'll probably have to just write the code for your page to accept GET requests as well as POST requests.
You can return the name of the view you want instead of the redirect, write as below:
[HttpPost]
public IActionResult AddProduct()
{
bool isFoo = handleBar();
if (isFoo)
{
return View();
}
return View("ProductList");
}
if you are using ajax you can redirect user in client side such as the following code:
$.ajax({
//settings
}).done(function (r) {
window.location.href = '/Product/ProductList';
});
or use success
$.ajax({
//settings
success: function(res) {
window.location.href = '/Product/ProductList';
}
});
Upvotes: 1
Reputation: 43860
When you use ajax you can not redirect on the server side. So you have two choices
Add form and submit button to your view, and use it to submit data. In this case you will be redirected from action on the server side.
Or fix your succes ajax part
success: window.location.href='@Url.Action("ProductList","Product")';
//or
success: window.location.href = "/product/productList" ;
Upvotes: 1