Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't the IgnoreDataMember attribute working when returning json from mvc?

I'm returning a JsonResult from an MVC controller action , and have been trying to remove one attribute but not having much joy.

return Json(db.Pages.ToList(), JsonRequestBehavior.AllowGet);

I have tried decorating my class that is being returned with the

[IgnoreDataMember]

attribute, and using [DataContract] and [DataMember] attributes on other attributes on the class but this seems to be ignored.

I found a post here where it states that returning json in this way will use that JavaScriptSerializer, I tried using [ScriptIgnore] but vs2010 doesn't recognise this as a valid attribute. http://teamezy.blogspot.com/2008/12/making-jsonresult-in-mvc-ignore.html

Do I need to return data in a different way in order for the IgnoreDataMember or DataContract / DataMember stuff to work?

like image 330
Tom Avatar asked Dec 06 '22 23:12

Tom


1 Answers

ScriptIgnoreAttribute is in the System.Web.Script.Serialization namespace - do you have an appropriate using directive and reference to the System.Web.Extensions assembly?

Remember to set ProxyCreationEnabled to false.

context.Configuration.ProxyCreationEnabled = false;
like image 188
Jon Skeet Avatar answered May 19 '23 18:05

Jon Skeet