JavaScriptSerializer [ScriptIgnore] not effective on virtual properties?

Baconbeastnz picture Baconbeastnz · Aug 18, 2012 · Viewed 7.3k times · Source

I have returning a Json(myObj) action result. The myObj is of type Badge

The only two objects that Badge has that could cause a loop from a serializer, are:

public class Badge
{
    public Badge() { }

    public Badge(String Name, String Description)
    {
        this.Name = Name;
        this.Description = Description;
    }

    [ScriptIgnore]
    public virtual BadgeType BadgeType { get; set; }
    [ScriptIgnore]
    public virtual ICollection<User> Users { get; set; }

    public int ID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string PrerequisiteCriteriaRef { get; set; }

    //PrerequisiteID

    public static Badge CreateForSeeder(BaseDBContext db, String Name, String Description, int TypeID)
    {
        Badge b = new Badge();
        b.Name = Name;
        b.Description = Description;
        b.BadgeType = db.BadgeTypes.Where(x => x.TypeID == TypeID).FirstOrDefault();
        return b;
    }
}

Which I've given the attribute, but it's not helping out at all...?

Answer

lxa picture lxa · Feb 23, 2015

You should set ApplyToOverrides parameter of ScriptIgnore to true:

[ScriptIgnore(ApplyToOverrides = true)]