ajax post to c# mvc controller
Hello
First time I've tried this, I am trying to make this ajax call do something before the form is submitted.
It seems to be working fine as far as it hits my breakpoint inside the AddPostalCode controller, but the postal code in the controller is null. (I'm not getting the value correctly) Any suggestions? Thanks
Ajax Call:
- <script>
- $('#submitPayment').click(function (e) {
- // TODO: Validate input
-
- var billingAddress = {
- PostalCode: $('#PostalCode').val().trim()
- };
-
- $.ajax({
- type: "POST",
- url: "/Reservations/Payment/AddPostalCode",
- content: "application/json; charset=utf-8",
- dataType: "json",
- data: JSON.stringify(billingAddress),
- success: function (data) {
- alert(data);
- },
- error: function () {
- alert("error");
- }
- });
- });
- </script>
Controller code:
- public class Data
- {
- public string PostalCode { get; set; }
- }
- [HttpPost]
- public JsonResult AddPostalCode(Data data)
- {
- var postalCode = data.PostalCode;
- // do stuff
- return Json(String.Format("'Success': 'true'"));
- }