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 9237119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:27:45+00:00 2026-06-18T07:27:45+00:00

I have a nested view model structure: public class PersonViewModel { public int Height

  • 0

I have a nested view model structure:

public class PersonViewModel
{
    public int Height { get; set; }
    public List<LegViewModel> Legs {get;set;}
}

public class LegViewModel
{
    public int Length { get; set; }
}

I send some JSON to this using jquery post:

<script>
    $(function () {

        $("#mybutton").click(function () {
            $.ajax({
                type: "POST",
                data: {
                    Height: 23,
                    Legs: [
                        {
                            Length: 45,
                        }
                    ]
                }
            });
        });
    });
</script>
<button id="mybutton">hello world!</button>

I’m posting to this controller action:

[HttpPost]
public ActionResult Save(PersonViewModel model)
{
    return Json(new { success = true });
}

The Height of a PersonViewModel gets populated, as does the number of elements in the Legs list, but each LegViewModel in the list does not: the Length property remains at 0 where I would expect the Legs array to contain one element with Length 45.

Note that this is also the same when I do not use a list at all: having the following yields a not null PersonViewModel.Legs property, but still as theLegs.Length` property as 0:

// view model
public class PersonViewModel
{
    public int Height { get; set; }
    //public List<LegViewModel> Legs {get;set;}
    public LegViewModel Leg { get; set; }
}

public class LegViewModel
{
    public int Length { get; set; }
}

// view
$("#mybutton").click(function () {
    $.ajax({
        type: "POST",
        data: {
            Height: 23,
            Leg: 
                {
                    Length: 45,
                }

        }
    });
})

How can I populate a nested view model using JSON? Is there something I’ve missed or can MVC not do this?

  • 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-18T07:27:47+00:00Added an answer on June 18, 2026 at 7:27 am

    If you want that the MVC Model Binder parses your collections properly when sending data with $.ajax you need to do two things:

    • Set the contentType to 'application/json'
    • And your data should hold JSON so JSON.stringify the data

    So here is the correct usage which then can be parsed by the model binder:

    $("#mybutton").click(function () {
            $.ajax({
                type: "POST",
                contentType: 'application/json',
                data: JSON.stringify({
                    Height: 23,
                    Legs: [
                        {
                            Length: 45,
                        }
                    ]
                })
            });
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model that looks like this: public class VenueIndexViewModel : BaseViewModel
I'm trying to get a nested model to bind correctly with a nested view
I have a nested class such as : public class Jar { public IEnumerable<NailClippings>
Suppose I have this model: public class ViewModel { [Required] public string UserInput {
I have Knockout.js view model that has nested observable arrays. function ParentVM(data) { var
I have a bus model, which has a nested route model. class Busables.Models.Bus extends
I have a JSON structure containing arrays of arrays. I have a View Model
I have a few nested view models with observable arrays and am unable to
I have nested list of the following form: my_list = [['Some1', '2', '3.6', '4.5',
Is it possible to have nested set capabilities in this somewhat custom setup? Consider

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.