How to display a warning message in the controller

I use one controller that inserts values ​​into the database. I want to display a warning message from the controller when the insertesd values ​​in the database are successful. Is it possible. If so, how?

+8
model-view-controller asp.net-mvc asp.net-mvc-2
source share
4 answers

This mainly depends on how you insert the value into the database, as you will need a method to tell you if the insert was successful. Since there are several ways to do this now, linq / entity framework / sql / etc.

Then, after you know whether the insert has occurred, you can simply assign the value to a variable, and then from the / aspx code just check the value and make a simple warning.

<script type="text/javascript"> //i'm using jquery ready event which will call the javascript chunk after the page has completed loading $(document).ready(function(){ //assuming that your variable name from the code behind is bInsertSuccess var bSuccess = "<%= bInsertSuccess %>"; if(bSuccess){ alert("Successfully Inserted"); } }); </script> 
+3
source share

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 opinion, 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 to make the information returned from the server unobtrusive. Verifying that ViewData ["Success"] or ViewData ["Failure"] is done on the main page, divs are formatted using CSS, and jQuery code was used to hide notifications after 5 seconds.

Hi,

Huske

+18
source share
 public ActionResult UploadPropertyImage() { // Business logic.... return Content("<script language='javascript' type='text/javascript'>alert('Save Successfully');</script>"); } 
+5
source share

You can add a code below to inform the user

 Return Content("Data added successfully"); 
0
source share

All Articles