views:

78

answers:

2

Hello, I have a master detail screen In ASP.NET MVC, when I submit something, It instantly returns to me a grid with the inserted value via Ajax, my problem is: I want to pass a message of failiure or successful via an jquery modal dialog or infobar and I can't pass a viewdata for the jquery to process It. Any ideas?

A: 

You can put this tag inside the script call.

var msg = '<%= ViewData["Mensagem"] %>';
alert(msg);

Don't forget to encode strings.

queen3
A: 

The action method that you are invoking through AJAX needs to return everything that's needed in the response. So for example when you put some object inside ViewData, this object will be available inside the View (or probably the partial view) that you are returning from the action and so available to the calling javascript. Another option is to return JSON object containing the message:

public ActionResult SomeAction()
{
    // ...
    return Json(
        new { Message = "Success Message!" }, 
        JsonRequestBehavior.AllowGet
    );
}

which could be invoked like this:

$.getJSON('/somecontroller/someaction', function(json) {
    alert(json.Message);
});
Darin Dimitrov