My MVC controller Message Box will not pop up on my redirect page

Viewed 19

I do not receive any errors in my controller. However, on my views page [alertMessage] does not exist in the current context. I wish there was a way to have the message box come up without having to redirect to a separate page but that is the only way I figured out how to do it. Any help would be great appreciated!

My controller Code

[HttpPost]
    public ActionResult AddAllergy(UserProfile userprofile, int UserAllergyId)
    {
      bool exists = _db.ProfileAllergies.Where(p => p.UserProfileId == userprofile.UserProfileId).Any(a => a.UserAllergyId == UserAllergyId);
      if(UserAllergyId != 0 && !exists)
      {
        _db.ProfileAllergies.Add(new ProfileAllergy() {UserAllergyId = UserAllergyId, UserProfileId = userprofile.UserProfileId});
        _db.SaveChanges();
      }
      else
      {
        return RedirectToAction("ThankYou");
      }
      return RedirectToAction("Details", new {id = userprofile.UserProfileId});
    }
    public ActionResult ThankYou()
    {
      TempData["alertMessage"] = "You've Already added this Allergy";
      return View();
    }

Thankyou.cshtml in my Views

@model AtchooClient.Models.UserProfile

@if(null != TempData["alertMessage"])
   {
      <script type="text/javascript">
      window.onload = function () {
         alert("@TempData[alertMessage]");
      };
      </script>
   }
0 Answers
Related