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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:57:19+00:00 2026-06-09T14:57:19+00:00

I don’t know why I’m just now noticing this behavior for the first time.

  • 0

I don’t know why I’m just now noticing this behavior for the first time. Looking to confirm whether this is designed behavior, or whether I’m missing something.

Say I have a viewmodel that implements IEnumerable<T>, and provides additional properties. For example:

public class MyResultsViewModel : IEnumerable<MyResultViewModel>
{
    public IEnumerable<MyResultViewModel> Results { get; set; }
    public string SomeAdditionalProperty { get; set; }

    public IEnumerator<MyResultViewModel> GetEnumerator()
    {
        return Results.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator; }
}

Say I also have a controller which returns this model as json. For example:

public ActionResult MyResults()
{
    var entities = PrivateMethodToGetEntities();
    var models = Mapper.Map<MyResultsViewModel>(entities);
    models.SomeAdditionalProperty = "I want this in the JSON too";
    return Json(models, JsonRequestBehavior.AllowGet);
    // models now contains a populated Results as well as the add'l string prop
}

When I get the results back from a JSON request on the client, it always comes back as an array. For example:

$.get('/Path/To/MyResults')
.success(function (results) {
    alert(results.SomeAdditionalProperty); // this alerts 'undefined'
    alert(results.length); // this alerts the size / count of Results
    alert(results[0]); // this alerts object
    // inspecting results here shows that it is a pure array, with no add'l props
});

Before I refactor to make the viewmodel not implement IEnumerable<T>, I want to get some confirmation that this is by design and should be expected. I guess it makes sense, since the javascript array object’s prototype would have to be extended to accommodate the additional properties.

Update:

I’ve made the following changes to the viewmodel, to avoid naming the internal enumerable Results:

public class MyResultsViewModel : IEnumerable<MyResultViewModel>
{
    public IEnumerable<MyResultViewModel> NotNamedResults { get; set; }
    public string SomeAdditionalProperty { get; set; }

    public IEnumerator<MyResultViewModel> GetEnumerator()
    {
        return NotNamedResults.GetEnumerator();
    }
    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator; }
}

With this change, the behavior remains. alert(JSON.stringify(results)) yields normal array syntax for my enumerated collection of MyResultViewModels:

[{“ResultProp1″:”A”,”ResultProp2″:”AA”,”ResultProp3″:”AAA”},{“ResultProp1″:”B”,”ResultProp2″:”BB”,”ResultProp3″:”BBB”},{“ResultProp1″:”C”,”ResultProp2″:”CC”,”ResultProp3″:”CCC”},{“ResultProp1″:”D”,”ResultProp2″:”DD”,”ResultProp3″:”DDD”},{“ResultProp1″:”E”,”ResultProp2″:”EE”,”ResultProp3″:”EEE”}]

Is still seems that the additional property is being lost between when the controller action returns the JsonResult and the jquery success function is invoked.

  • 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-09T14:57:20+00:00Added an answer on June 9, 2026 at 2:57 pm

    For classes implementing IEnumerable, the JavascriptSerializer only serializes the enumerated items. It calls GetEnumerator() to get the data to be serialized, any other properties are ignored. That’s why the Count property of a List<T> is not serialized, nor will any other property.

    The reason is that this type of construction cannot be represented in json format. To include the other properties the serializer would have to create a hash object instead of an array, but a hash object is not a collection, strictly speaking. (The exception are key/value pair classes like Dictionary, which are serialized as hash objects. But the rule stands – only the enumerated dictionary entries are serialized).

    But why are you creating a class that implements IEnumerable directly to serialize as json instead of doing this?

     public class MyResultsViewModel {
         public IEnumerable<MyModel> Models{ get; set; }
         public String SomeAdditionalData { get; set; } 
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know whether I'm having a "thick day" - but I just wondered what
I don't know why, but this code worked for me a month ago... maybe
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know if this has been asked before, so point me to another question
Don't know if anyone can help me with this or if it's even possible.
Don't know whats exactly going on, but it's definitely killing my time for nothing.
I don't know: if this works. if it's a good idea. what it is
I don't know if this question is trivial or not. But after a couple
Don't know why this doesn't work. I can't do implicit animation for a newly
Don't know a whole lot about streams. Why does the first version work using

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.