3

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...?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
williamsandonz
  • 15,864
  • 23
  • 100
  • 186

3 Answers3

13

You should set ApplyToOverrides parameter of ScriptIgnore to true:

[ScriptIgnore(ApplyToOverrides = true)]
lxa
  • 3,234
  • 2
  • 30
  • 31
4

The problem here is that your virtual parameters are override into a Dynamic created class. In this case the [ScriptIgnore]attributes are overriden also.

that is why you should use [ScriptIgnore(ApplyToOverrides = true)], in order to keep the [ScriptIgnore] attribute valid into derived classes.

Daniel Santos
  • 14,328
  • 21
  • 91
  • 174
1

The JavaScriptSerializer (which is what is used when you return Json) definitely honors the [ScriptIgnore] attribute.

Here's a proof:

Model:

public class User
{
    public Badge Badge { get; set; }
}

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

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

Controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var badge = new Badge
        {
            ID = 1,
            Name = "badge"
        };
        var user = new User
        {
            Badge = badge
        };
        badge.Users = new[] { user }.ToList(); 

        return Json(badge, JsonRequestBehavior.AllowGet);
    }
}

If you remove the [ScriptIgnore] attribute from the Users property you will get a circular reference error.

So I guess that your problem is somewhere else.

But personally I would recommend you using view models instead of those [ScriptIgnore] attributes.

So you simply define a view model that will contain only the properties you need for the given view:

public class BadgeViewModel
{
    public int ID { get; set; }
    public string Name { get; set; }
}

and then in your controller action you map between the domain model and the view model and you pass the view model to the view:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        Badge badge = ...
        BasgeViewModel vm = new BasgeViewModel
        {
            Id = badge.Id,
            Name = badge.Name
        };

        return Json(vm, JsonRequestBehavior.AllowGet);
    }
}

and if you are sick of writing mapping code in your controllers you simply head over to your NuGet package console and type the following command:

Install-Package AutoMapper

in order to take full advantage of the excellent AutoMapper library.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • For some reason I thought View models were a redundant approach, I'll read some pages, do some research. Clear I'm wrong, sounds like it could be JUST what I need. To solve THIS problem & issues I'm having with my validation (which you also commented on). Thank you Darin, great post! – williamsandonz Aug 18 '12 at 08:32