Hello I'm a bit stuck to display an alert msg with latest version of asp.net core 8 MVC. I have an edit post form with Ajax and this is the code.
View code:
@{
ViewData["Title"] = "Edit book";
var notification = TempData["Notification"];
}
@if (notification != null)
{
<div class="alert alert-danger" role="alert">
@notification
</div>
}
// Ajax script
var url = '@Url.Action("EditBook", "Account")';
$.ajax({
cache: false,
async: true,
type: "POST",
url: url,
data: {
model
},
success: function (response, xhr) {
if (response.redirectToUrl != undefined) {
window.location.href = response.redirectToUrl;
} else {
}
}
});
The end of my POST controller where I manage my alerts
if (result == 1)
{
TempData["Notification"] = "The book has been published successfully!";
return Json(new { redirectToUrl = Url.Action("Books", "Account") });
} else // Error
{
TempData["Notificacion"] = "There was an error processing your request, please contact with support";
return View(model);
}
Doing this with successful message works good because it is a redirect but I don't want to loose the model and that's why I'm trying to return the same view with model.
Edit Fixed was my gallery js breaking it. After disable, it works now.