0

I am getting this error when the datsource model have sitecore image field. the below code written in the .cshtml file and the model is binding via Glass mapper.

<script>
 var var1 = @Html.Raw(Json.Encode(Model));
</script>

any help to resolve this issue

2
  • Do not serialize the whole Model. Serialize only what you need from that model
    – Marek Musielak
    May 19 at 11:53
  • model is common and have so many fields. if any of the field is sitecore Image type, problem is coming
    – shahul
    May 22 at 9:40

2 Answers 2

1

Solution above Sumit provided will also works for you, you can try below things as well.

  1. Apply [JsonIgnore] to navigational properties

  2. In global.asax GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;

  3. Serialize the data

string json = JsonConvert.SerializeObject(gridDataResult.GridData, Formatting.None);
return Json(json, JsonRequestBehavior.AllowGet);

Hope this as well and do not serialize whole modal only serialize whatever needed from modal.

Hope this helps.

1
  • I have to use Sitecore "Image" data type. I don't have control to modify any properties to add [JsonIgmore].
    – shahul
    May 24 at 5:52
0

It seems that there are circular references in your object hierarchy which is not supported by the JSON serializer. You could pick up only the properties you need in the view:

I would suggest you to create a class like this for Image and bind the image properties.

public class DataItemImage
{
    public string ID { get; set; }
    public string Source { get; set; }
    public string Width { get; set; }
    public string Height { get; set; }
    public string Alt { get; set; }

    public DemoDataItemImage(Sitecore.Data.Fields.ImageField obj)
    {
        string mediaUrl = Sitecore.Resources.Media.MediaManager.GetMediaUrl(obj.MediaItem);
        ID = obj.MediaItem.ID.ToString();
        Source = mediaUrl;
        Width = obj.Width;
        Height = obj.Height;
        Alt = obj.Alt;
    }
}

And then use this to create the Json object.

Hope this helps.

2
  • Yes this may work but currenlty I am using glass mapper and Image field to map sitecore fields. is there any way to use same without circular issue
    – shahul
    May 22 at 9:43
  • @shahul Did you try this? var list = JsonConvert.SerializeObject(model, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore }); May 22 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.