Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi,

Filling a form like Gmail registration, I want to display alert message box after saving the data, I think this can be done from Controller, so please any idea?
Posted

Hi, Add this code in view
XML
@{
    if (TempData["message"] != null) {
    <script type="text/javascript">
        alert(@Html.Raw(Json.Encode(TempData["message"])));
    </script>
}
    }



SQL
[HttpPost]
        public ActionResult Submit()
        {
            if (ModelState.IsValid)
            {
               // Do your stuff
            TempData["message"] = "Your Message";
            }


            return View("ViewName");
        }
 
Share this answer
 
Comments
Faisalabadians 12-Sep-13 9:29am    
Why TempData instead of ViewBag?
 
Share this answer
 
v2
Comments
Rockstar_ 12-Sep-13 3:20am    
I'm filling the details in a form and i want to display the message on the same page after save click button, so the above link, displays alert message for while loading the form first time also.
[no name] 12-Sep-13 3:23am    
try the second link..
Hi,

You can use Ajax.BeginForm. Which provides you AjaxOptions. Using that, you can fire javascript function after success/failure of your controller action.

Here is one example.

@Ajax.BeginForm("ActionName","ControllerName",null,new AjaxOptions(){OnSuccess = "javascript:alert('Login Successfully!');"},new{})


--SDK
 
Share this answer
 
Comments
Faisalabadians 12-Sep-13 9:30am    
What if I user html.beginform? ajax form is not the only option, this could be achieve in html form too.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900