Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7748277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T10:44:33+00:00 2026-06-01T10:44:33+00:00

I’m currently trying to post a JSON object from my view to my controller

  • 0

I’m currently trying to post a JSON object from my view to my controller in an MVC 3 app and I’m expecting the data from the request to bind to the model parameter of my controller action. However, when I inspect the HTTP POST request, it looks like the data is being passed as a query string and I’m unsure why (pretty new to all this).

Here’s my POST request:

    $.ajax({
        type: 'POST',
        url: "Test/Add",
        data: { Name: name, Age: age },
        success: function (data) {
            console.log("success");
        },
        error: function (xhr, data, message) {
            console.log(data + ": " + message);
        },
        dataType: "json"
    });

Here’s the code from my Controller followed by the code for the model I’m trying to bind to:

    [HttpPost]
    public ActionResult Add(PersonModel person)
    {
        System.Threading.Thread.Sleep(1000);

        return View();
    }

    // person model:
    public class Person {

        public string Name {get;set;}
        public int Age {get;set;}

    }

This is the request from fiddler – I’ve highlighted the parts I’m unsure about:

enter image description here

I thought content type would be “application/json” and that the data wouldn’t look like a query string – I thought it would look something like this:

{
    Name: "James",
    Age: 13
}

Ultimately the problem is that if I stick a breakpoint in my controller action I expect to see a populated Person object but it’s always null. If I replace the signature to be something like (object name, object age) then I get values for the parameters (a string array for both – so name is a string array with 1 element which is equal to “James”).

Any ideas where I’ve gone wrong?

Oh and just FYI, I’m not actually 13! Those were the first numbers I mashed.

Thanks!

James

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T10:44:35+00:00Added an answer on June 1, 2026 at 10:44 am

    @David is correct in that you need to set the contentType to ‘application/json’. But you also need to send JSON. You are sending an object over. To convert it to JSON use JSON.stringify. I tried sending it over as an object with this contentType setting and received a server error. I actually got it to work correctly without setting the contentType. Even though the request came across as “Name=James&Age=13” MVC was able to convert it into an object. I modified your test to echo back the results as JSON and display them.

    Here is the modified Controller

            [HttpPost]
        public string Add(PersonModel person)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            string result = serializer.Serialize(person);
    
            return result;
        }
    
        // person model:
        public class PersonModel
        {
    
            public string Name { get; set; }
            public int Age { get; set; }
    
        }
    

    The modified Javascript and HTML looks like this:

    <input id="doAjax" type="button"  value="Send Json..."/>
    <br />
    Results:<br />
    <textarea id="results" cols="30" rows="3"></textarea>
    <script type="text/javascript">
    
     $( document ).ready( function () {
        $( "#doAjax" ).click( sendJson );
    } );
    
    function sendJson() {
        var name = "James";
        var age = "13";
        var person = { Name: name, Age: age };
        var personJson = JSON.stringify( person );
        $.ajax( {
            type: 'POST',
            url: "Test/Add",
            data: personJson,
            contentType: 'application/json',
            success: function ( data ) {
                var jsonStr = JSON.stringify( data );
                $( "#results" ).val( jsonStr );
            },
            error: function ( xhr, data, message ) {
                var jsonStr = JSON.stringify( data );
                $( "#results" ).val( jsonStr + ": " + message );
            },
            dataType: "json"
        } );
    }
    

    Here are the results of some tests:

    data = person; contentType = not set; request = “Name=James&Age=13”; response = “{“Name”:”James”,”Age”:13}”

    data = personJson; contentType = not set; request = “{“Name”:”James”,”Age”:”13″}”; response = “{“Name”:null,”Age”:0}”

    data = person; contentType = ‘application/json’; request = “Name=James&Age=13”; response = “System.ArgumentException: Invalid JSON primitive: Name”

    data = personJson; contentType = ‘application/json’; request = “{“Name”:”James”,”Age”:”13″}”; response = “{“Name”:”James”,”Age”:”13″}”

    Note that when the contentType is not set it defaults to ‘application/x-www-form-urlencoded’

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.