Hi, I am trying to return IEnumerable<IPublishedContent> from controller with return Json(allOffers, JsonRequestBehavior.AllowGet); But it seems that Umbraco's IPublishedContent can not be serialized to json. I got error A circular reference was detected while serializing an object of type 'Umbraco.Web.Models.DynamicPublishedContent'. Anyone knows solution ?
I have seen the example u linked mate but id didn't worked for me.
After you said that u had already used this example and it works I copied whole example and try to make it work. At last i got perfectly working controller with ajax request which returns json response. Here is code if anyone will need it in future :
public class OffersSurfaceController : SurfaceController
{
public ActionResult GetFilteredOffers(int ? offersTypeId, int ? offersCategoryId)
{
IEnumerable<IPublishedContent> allOffers = Umbraco.Content(1362).Children();
if (offersCategoryId != 0 && offersCategoryId != null)
{
allOffers = allOffers.Where(x => x.GetPropertyValue<int>("offersItemCategory") == offersCategoryId);
}
if (offersTypeId != 0 && offersTypeId != null)
{
allOffers = allOffers.Where(x => x.GetPropertyValue<int>("offersItemType") == offersTypeId);
}
return Json(allOffers.Select(l => new { text = l.GetPropertyValue("offersItemTitle") }), JsonRequestBehavior.AllowGet);
}
}
Notice allOffers.Select(l => new { text = l.GetPropertyValue("offersItemTitle") }) makes code working, ToList(), ToArray() gives error which i mentioned above.
Umbraco IPublishedContent to json
Hi, I am trying to return
IEnumerable<IPublishedContent>
from controller withreturn Json(allOffers, JsonRequestBehavior.AllowGet);
But it seems that Umbraco's IPublishedContent can not be serialized to json. I got errorA circular reference was detected while serializing an object of type 'Umbraco.Web.Models.DynamicPublishedContent'.
Anyone knows solution ?Hi,
You're probably using some method that returns a dynamic, switch to a typed one. This example link I already used and works.
https://dejanstojanovic.net/umbraco-cms/2014/august/fastest-way-to-return-json-result-from-a-controller/
Hi Marcio.
I have almost exact code, here it is :
And i changed it 100 times, tried everything but i always get same error:
The error flashes when something comes from database, when it is empty it works and logs empty array in console(I am using ajax request).
I have seen the example u linked mate but id didn't worked for me. After you said that u had already used this example and it works I copied whole example and try to make it work. At last i got perfectly working controller with ajax request which returns json response. Here is code if anyone will need it in future :
Notice
allOffers.Select(l => new { text = l.GetPropertyValue("offersItemTitle") })
makes code working,ToList(), ToArray()
gives error which i mentioned above.Thanks to Marcio.
Hi Saba,
Ensure that this
is this
they keyword being Typed, this makes the returned object strongly typed as an IPublishedContent
I suggests use the examine to kind of filter. I will share similar code i have done some time ago, coming soon
I am looking forward for it, thanks in advance :)
is working on a reply...