2

I have problem with passing string value from jQuery to C# in Web MVC.

There is my jQuery code:

$.ajax({
    type: 'POST',
    url: '/Account/ChangePhoneNumber',
    contentType: "application/json; charset=utf-8",
    data: newPhone,
    dataType: 'json',
    success: function (result) {
        alert("We returned: " + result);
    }
})

Here is my C# method:

[HttpPost]
public ActionResult ChangePhoneNumber(string data)
{                            
    return View();
}

While debugging data parameter is always null. What I am doing wrong?

1
  • data: {data: newPhone}, Commented Dec 22, 2015 at 10:00

6 Answers 6

2

Ok I`ve figured out what was wrong:

 $.ajax({
                                type: 'POST',
                                url: '/Account/ChangePhoneNumber',
                                contentType: "application/json; charset=utf-8",
                                data: JSON.stringify({data: newPhone}),
                                dataType: 'json',
                                success: function (result) {
                                    alert("We returned: " + result);
                                }
                            })

I had to add data: JSON.stringify({data: newPhone}),

Sign up to request clarification or add additional context in comments.

Comments

0

You were almost there;

$.ajax({
        type: 'POST',
        url: '/Account/ChangePhoneNumber',
        contentType: "application/json; charset=utf-8",
        data: {data: newPhone, foo: bar, fooz: barz},   //<-- mind the array here
        dataType: 'json',
        success: function (result) {
                     alert("We returned: " + result);
                  }
        })

Which will result the following;

[HttpPost]
public ActionResult ChangePhoneNumber(string data, string foo, string fooz)
{ 
    //data will contain newPhone
    //foo will contain bar
    //fooz will contain barz                           
    return View();
}

Just make sure to put the object in an array. So, in your case:

data: {data: newPhone},

will solve your problem.

Comments

0

You are not passing data correctly in ajax call. You need to pass it like this data: {data: newPhone},

 $.ajax({
            type: 'POST',
            url: '/Account/ChangePhoneNumber',
            contentType: "application/json; charset=utf-8",
            data: {data: newPhone},
            dataType: 'json',
            success: function (result) {
                 alert("We returned: " + result);
            }
        })

 [HttpPost]
 public ActionResult ChangePhoneNumber(string data)
 {                            
      return View();
 }

Comments

0

you need post your data as form object; some useful information can found here

                 $.ajax({
                            type: 'POST',
                            url: '/Account/ChangePhoneNumber',
                            contentType: "application/json; charset=utf-8",
                            data: {data: newPhone},
                            dataType: 'json',
                            success: function (result) {
                                alert("We returned: " + result);
                            }
                        })

2 Comments

Still doesn't work is var newPhone = $('#newPhone1').val(); this ok ?
yes if your input is <input id="newPhone1" type="text" />
0

Check how you defined the route.

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{data}",
                defaults: new { controller = "yourcontroller", action = "actionname", data = UrlParameter.Optional }
            );

Please check if the parameter name in your route is "data"

Comments

0

If you are not worried about AJAX/JSON, you can simply use $.Post method.

var url = "@Url.Action("ChangePhoneNumber", "Account")";
        $.post(url, { data: newPhone })
            .fail(function() {
                // do something
            })
            .done(function(result) {
                alert("We returned: " + result);
            });

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.