Don't want to JSON serialize the whole list of classes

Posted by mjmcloug on Stack Overflow See other posts from Stack Overflow or by mjmcloug
Published on 2010-03-09T11:40:32Z Indexed on 2010/03/09 11:51 UTC
Read the original article Hit count: 359

Filed under:
|
|
|

Hey,

I've got a IList of Sites in my application and Site has a large amount of properties.

I'm wanting to convert this list to JSON to be used in a dropdownlist similar to this

    var sites = SiteRepository.FindAllSites();
    return new JsonResult() { Data = sites, JsonRequestBehavior = JsonRequestBehavior.AllowGet };

the problem I have is that I only want to use the id and name properties of the site class. I was thinking a way round this would be to use an 'adaptor' class that would then only expose these two properties and I would then serialize that.

The problem I have is I want to make the class generic so that it can handle any list of objects. Has anybody come across a similar situation and solved it?

© Stack Overflow or respective owner

Related posts about c#

Related posts about JSON