Toast notifications in ASP.NET MVC 4
Answer 1:
<script type="text/javascript">
$(document).ready(function () {
if('@TempData["message"]' == "Added"){
toastr.success('Added');
}
else{ }
});
</script>
Answer 2:
Although TempData
retain its value on one redirect but sometimes it creates problem(and it is recommended to avoid using TempData
) in that case you can do as:
public ActionResult AddToCart(int id)
{
.........
return RedirectToAction("Index", new { message="Added" }); //Send Object Route//
}
public ActionResult Index(string message)
{
.........
if(!string.IsNullOrEmpty(message)) {
Viewbag.message=message;
}
return View();
}
<script type="text/javascript">
$(document).ready(function () {
if('@Viewbag.message' == "Added") {
toastr.success('Added');
}
else{ }
});
</script>