0

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

2
  • Why did you alert('Data Sent'); instead of you should alert the return data. Commented Dec 12, 2013 at 7:08
  • it cannot be Deserialize when you not throw some key in your json. Commented Dec 12, 2013 at 7:09

2 Answers 2

3
$.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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

it did not work when i try to get it still say reason is null.
My debugger was skipping the lines so when breakpoint at every line it work.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.