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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:26:41+00:00 2026-05-20T03:26:41+00:00

Good evening everyone. I am currently using MVC 3 and I have a viewmodel

  • 0

Good evening everyone. I am currently using MVC 3 and I have a viewmodel that contains a property that is a List. I am currently using json2’s JSON.stringify method to pass my viewmodel to my action method. While debugging I am noticing that all the simple properties are coming thru but the collection property is empty even though I know for sure that there is at least one object in the collection. I wanted to know if there is anyone that is running into the same issue. Below is the code that I am using to post to the action method:

$.post("/ReservationWizard/AddVehicleToReservation/",
        JSON.stringify('@ViewData["modelAsJSON"]'),
        function (data) {
            if (data != null) {
                $("#vehicle-selection-container").html(data);
                $(".reservation-wizard-step").fadeIn();
            }
        });

The object @ViewData["modelAsJSON"] contains the following json and is passed to my action method
{"NumberOfVehicles":1,"VehiclesToService":[{"VehicleMakeId":0,"VehicleModelId":0}]}

As you can see the property “VehiclesToService” has one object but when it gets to my action method it is not translated to the corresponding object in the collection, but rather the collection is empty.

If anyone has any insight into this issue it would be greatly appreciated.

Thanks in advance.

UPDATE

OK after making the recommended changes and making the call to new JavaScriptSerializer().Serialize(@Model) this is the string that ultimately gets sent to my action method through the post
'{"NumberOfVehicles":1,"VehiclesToService":[{"VehicleMakeId":0,"VehicleModelId":0}]}'

I can debug and see the object that gets sent to my action method, but again the collection property is empty and I know that for sure there is at least one object in the collection.

The AddVehicleToReservation action method is declared as follows:
public ActionResult AddVehicleToReservation(VehicleSelection selections)
{
...
return PartialView("viewName", model);
}

  • 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-05-20T03:26:41+00:00Added an answer on May 20, 2026 at 3:26 am

    Here’s the problem:

    JSON.stringify('@ViewData["modelAsJSON"]')
    

    JSON.stringify is a client side function and you are passing as argument a list that’s stored in the ViewData so I suppose that it ends up calling the .ToString() and you have

    JSON.stringify('System.Collections.Generic.List<Foo>')
    

    in your final HTML which obviously doesn’t make much sense. Also don’t forget that in order to pass parameters to the server using the $.post function the second parameter needs to be a javascript object which is not what JSON.stringify does (it generates a string). So you need to end up with HTML like this:

    $.post(
        'ReservationWizard/AddVehicleToReservation',
        [ { id: 1, title: 'title 1' }, { id: 2, title: 'title 2' } ],
        function (data) {
            if (data != null) {
                $('#vehicle-selection-container').html(data);
                $('.reservation-wizard-step').fadeIn();
            }
        }
    );
    

    So to make this work you will first need to serialize this ViewData into JSON. You could use the JavaScriptSerializer class for this:

    @{
        var myList = new JavaScriptSerializer().Serialize(ViewData["modelAsJSON"]);
    }
    $.post(
        '@Url.Action("AddVehicleToReservation", "ReservationWizard")',
        // Don't use JSON.stringify: that makes JSON request and without
        // proper content type header your sever won't be able to bind it
        @myList,
        function (data) {
            if (data != null) {
                $('#vehicle-selection-container').html(data);
                $('.reservation-wizard-step').fadeIn();
            }
        }
    );
    

    And please don’t use this ViewData. Make your views strongly typed and use view models.

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

Sidebar

Related Questions

Good evening, In my app that I'm currently developing, I have a class that
Good evening everyone, I am using a JavaScript to load/override content from an HTML-File
Good evening everyone. I am puzzling over the oddity that the jQuery Marquee plugin
Good evening/morning/after/noon. I have an ASP.net 3.5 website and I am using vb.net in
Good evening/morning/after/noon. I have an ASP.net 3.5 website and I am using vb.net in
Good evening everyone! I am working on learning some java and I have made
Good evening, here is what I am trying to achieve, I currently have a
Good Evening, The problem is that i have both xcode 3.2 and xcode 4
Good morning folks, I'm just that beginner in json, I have a formated data
Good morning. I have an XML file which contains lists of warning and errors

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.