Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to display alert message in controller

I am using one controller which is inserting values in the database. I want to display alert message from controller when the values insertesd in the database successfully. Is it possible. If yes then how?

like image 943
Saloni Avatar asked Jun 24 '11 05:06

Saloni


2 Answers

You can add the result to ViewData. For example:

if (SaveToDbOK)
{
    ViewData["Success"] = "Data was saved successfully.";
   // Do other things or return view
}

In your view you can place anywhere:

MVC2:

<% if (ViewData["Success"] != null) { %>
    <div id="successMessage">
        <%: ViewData["Success"] %>
    </div>
<% } %>

MVC3:

@if (ViewData["Success"] != null) {
    <div id="successMessage">
        @ViewData["Success"]
    </div>
@}

I used this approach in my last project in order to make the information returned from the server unobtrusive. Checking whether ViewData["Success"] or ViewData["Failure"] are done in the Master page, the divs are formatted using CSS, jQuery code was used to hide the notifications after 5 seconds.

Regards,

Huske

like image 176
Husein Roncevic Avatar answered Oct 15 '22 12:10

Husein Roncevic


public ActionResult UploadPropertyImage()
{
    // Business logic....
    return Content("<script language='javascript' type='text/javascript'>alert('Save Successfully');</script>");
}
like image 26
shahnila Avatar answered Oct 15 '22 12:10

shahnila