Mohit
Mohit

Reputation: 2217

Retrieving Value post from $.ajax in ASP.NET MVC 4 Controller Method

I have write down a script as following

  var reason = prompt("Please Enter the Reason", "");
                            if(reason != null)
                            {
                               // alert('you are here');


                                $.ajax({
                                    type: "POST",
                                    url: "/Controller/ActionMethod",
                                    content: "application/json; charset=utf-8",
                                    dataType: "json",
                                    data: Json.stringify(reason),
                                    success: function(){ alert('Data Sent');}

                                });
                            }

which works fine as it calls the ActionMethod from the controller but i am not being able to retrieve data taken with the help of prompt. within the controller.

I have tried

String reason = Request["reason"];

as well as i have tried to pass the data as argument in controller

public ActionResult ActionMethod(int id, string reason)

but in all cases reason is null. please tell me how can i retrieve the reason from the same.

thanks in advance

Upvotes: 0

Views: 3256

Answers (2)

ebattulga
ebattulga

Reputation: 10981

$.ajax({
        type: 'POST',
        url: "/Controller/ActionMethod",                  
        data:{'reason':reason},
        dataType: 'json',
        success: function(jsonData) {
        },
        error: function(error) {

        }
    });

Your action maybe like this

[HttpPost]
public ActionResult ActionMethod(string reason){
...
return Json(obj);
}

Upvotes: 3

SHM
SHM

Reputation: 1952

this should work: Request.Form["reason"].

Upvotes: 1

Related Questions