1

I am using MVC 3 Razor for my website. My requirement is that I want to show display friendly messages to the user on Success/Failure of the request, on the View itself, don't want to re direct the user to another View. I want like I could customize the display like Green color with some Success image and Red color for failure and respective image.

What would be the best approach?

skaffman
  • 398,947
  • 96
  • 818
  • 769
vivek
  • 11
  • 2
  • 1
    It's a bit unspecific. What do you already have? – DanielB May 31 '11 at 08:11
  • I'm interested in best practices for *where* the code should be. For example, you can return a JavaScriptResult from your action method (`return this.JavaScript(@"jQuery('#foo').fadeIn(2000).delay(10000).fadeOut(5000);");`) or you can use events from an AJAX request. But ultimately you need code somewhere that shows and hides the message. The code is somewhat specific to the view, but embedding JavaScript in a view seems to be mixing responsibilities of controller and view... Perhaps you should have a global JS file with a very specific function or method to call for this view..? – bambams Feb 15 '12 at 22:02

1 Answers1

0

It sounds like you want to know if a request failed or succeeded based on user input. If you are posting data to the server, you want to consider using the AjaxHelper the BeginForm-method takes an argument of AjaxOptions.

On the AjaxOptions you can specify OnSuccess and OnError among others and when one of these are called, you can call the corresponding JavaScript.

If this on the other hand is not enough, you might want to inspect the data that came from the server and based on that decide if the request succeeded or not, you want to use jQuery and their ajax-components. Here's an example of how to use the jQuery-option:

$.post("/MyController/MyAction", function(data) {
   // Check what data contains.
 });
Filip Ekberg
  • 36,033
  • 20
  • 126
  • 183
  • Well I am not using Ajax calls every where. Lets take a simple example I have a Registration Form say the data was not saved to database (server side call to Action in my controller) basically form is posted back. The response is Registration failed due to say email id already registered. This I want to show to the user. – vivek May 31 '11 at 09:34
  • You can do this the way I described above and you can also use data-annotations. Check this article out: http://www.asp.net/mvc/tutorials/validation-with-the-data-annotation-validators-cs you should also read this article http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx this should get you going, tell me if you still can't solve it! – Filip Ekberg May 31 '11 at 09:57
  • I believe my requirement is perceived in some other way. I just want to show say " Record added successfully" to my user. – vivek May 31 '11 at 10:54
  • Then you should use `AjaxOptions` and use the `OnSuccess`-event and fire a JavaScript accordingly. Here's an example http://stackoverflow.com/questions/1200960/asp-net-mvc-ajaxoptions-onsuccess-fires-too-early – Filip Ekberg May 31 '11 at 15:35
  • @vivek, did it turn out as you'd like? – Filip Ekberg Jun 01 '11 at 07:24