sending data through JSON to MVC Controller Method

Hi All,

I've trying to send across some data to a MVC model.

The model is calling but the data is null? any ideas what I'm doing wrong here'

var fields = new Array();

                    "amount": $('#amountSlider').val(),
                    "duration": $('#durationSlider').val(),
                    "security": $('#currentSecuritySlider').val(),
                    "loan": $('#existingLoansSlider').val(),
                    "securityType": $('#securityTypeDropDown').val(),
                    "charge": $('#chargeDropDown').val(),

                    //second page
                    "fullName": $('#fullNameTextBox').val(),
                    "email": $('#emailTextBox').val(),
                    "phone": $('#phoneTextBox').val()


                    url: "/Home/SendForm",
                    contentType: "application/x-www-form-urlencoded",
                    type: "POST",
                    datatype: "json",
                    data: JSON.stringify(fields),
                    error: function (xmlHttpRequest, errorText, thrownError) {
                        alert(xmlHttpRequest + "|" + errorText + "|" + thrownError);
                    success: function (data) {

Open in new window

and the method in my home controller

        public class InstantQuote
            public string amount { get; set; }
            public string duration { get; set; }
            public string security { get; set; }
            public string loan { get; set; }
            public string securityType { get; set; }
            public string charge { get; set; }
            public string fullName { get; set; }
            public string email { get; set; }
            public string phone { get; set; }


        public Boolean SendForm(InstantQuote form) {

Open in new window

When I break into the c# method its showing form contains an object but all the items are set to null?
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
Your Javascript is passing an array, but your action isn't expecting an array. Change your Javascript to create a new object, and that object will have property names that correspond to the InstantQuote class.
käµfm³d 👽Commented:
What does your action's signature look like?
flynnyAuthor Commented:
Thanks fo rthe fast reply.

As above its just  [HttpPost]
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

flynnyAuthor Commented:
Thanks Kaufmed,

that worked.

so in order for MVC to convert the JSON to the model it needs to be explicitly sent as an object rather than a stringified array?

how does the ajax know this is the JSON.stringify not the same string?
käµfm³d 👽Commented:
No. You need to match the structure between your C# and your Javascript. You can assuredly send an array from Javascript to C#...if your C# expects an array. So you could have alternatively changed your C# to:

public Boolean SendForm(InstantQuote[] form) {

Open in new window

If you actually intend on sending an array, then you'd want the above instead. I didn't gather that you actually needed an array.
käµfm³d 👽Commented:
Also, IIRC, you shouldn't need to stringify the data in order to send it. Just send the JSON object in the data property of your AJAX call.
flynnyAuthor Commented:
Hi KAufmed,

no i dont need an array I was just wondering how it would be able to tell the difference as the JSON.stringify string of both objects would be the same. (mainly for understanding as you solution worked perfectly.

i just did a console output on both sets and I see now that stringifying the array encases it in []

thanks again kaufmed
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.