Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use message box in MVC controller?

I've created a MVC application. User has to register and once it is completed, I'm redirecting him to "thank you" page. However I would like just to show user a pop-up with this message. How can I achieve this?

My code:

[HttpPost]
public ActionResult Enquiry(Enquiry enquiry)
{
    if (ModelState.IsValid)
    {
        dbEntities.Enquiries.AddObject(enquiry);
        dbEntities.SaveChanges();
        enquiry.SendEnquiryEmail(enquiry);

        return RedirectToAction("Thankyou"); 
    }

    return View(enquiry);
}

//redirect to thankyou page
public ActionResult Thankyou()
{
    return View();
}
like image 295
Neo Avatar asked Mar 10 '12 12:03

Neo


1 Answers

To ensure your 'alert' in your view only shows up when you intend (a redirect from your ThankYou method) and not when somebody accidentally navigates to your 'ThankYou' view

//redirect to thankyou page
public ActionResult Thankyou()
{
    TempData["alertMessage"] = "Whatever you want to alert the user with";
    return View();
}

Then in your "ThankYou" view, this:

   if(null != TempData["alertMessage"])
   {
      <script type="text/javascript">
       alert("@TempData[alertMessage]");
      </script>
   }

This will write out the script as you normally would for any JavaScript. Hope this helps!

like image 125
Reynolds Avatar answered Sep 18 '22 23:09

Reynolds