Hi rani,
Check this example. Now please take its reference and correct your code.
Controller
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
[HttpPost]
public IActionResult Save()
{
TempData["Message"] = "You clicked Save!";
return RedirectToAction("Index");
}
[HttpPost]
public IActionResult Cancel()
{
TempData["Message"] = "You clicked Cancel!";
return RedirectToAction("Index");
}
}
View
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form asp-controller="Home">
<input type="submit" asp-action="Save" value="Save" />
<input type="submit" asp-action="Cancel" value="Cancel" />
</form>
@if (TempData["Message"] != null)
{
<script type="text/javascript">
window.onload = function () {
alert('@TempData["Message"]');
};
</script>
}
</body>
</html>
Screenshot