Travis, I know that you have your accepted answer here, but I wanted to talk a little about it. Recently, I ran into very similar problems and couldn’t make it work for me, tried all the attributes [scriptignore], etc. Etc.
What ultimately worked for me was using Automapper and creating a map from a proxy object to a slimmed down poco object. This allowed me to solve all my problems within 2 minutes. All this after a 36-hour siege that prevailed when trying to force a proxy to play ball-shish :-)
Another approach to thinking in the interim.
[Change] - using Automapper (this is a small test application that refers to automapper)
ref: http://automapper.codeplex.com/
nuget: Install-Package AutoMapper
Classes:
public sealed class One : BaseViewModel { // init collection in ctor as not using EF in test // no requirement in real app public One() { Two = new Collection<Two>(); } public int OneId { get; set; } public ICollection<Two> Two { get; set; } } public class Two { public int TwoId { get; set; } public int OneId { get; set; } [ScriptIgnore] public virtual One One { get; set; } } public abstract class BaseViewModel { public string AsJson() { var serializer = new JavaScriptSerializer(); return serializer.Serialize(this); } } public class OnePoco : BaseViewModel { public int OneId { get; set; } public virtual ICollection<TwoPoco> Two { get; set; } } public class TwoPoco { public int TwoId { get; set; } public int OneId { get; set; } }
test controller code:
public ActionResult Index() {
give it a try and see how you do it, it certainly worked for me, the earth has moved :)
source share