0

On the client side an object 'selectedMeter' is build in JavaScript. This object is send to the server with an ajax call and passed to the mvc controller.

$.ajax({
   url: 'SaveManualMeter',
   type: "POST",
   cache: false,
   contentType: "application/json; charset=utf-8",
   data: **JSON.stringify({ clientId: selectedClientId, manualMeter: selectedMeter** }),
   complete: function () {

When tested locally, it works, however when the site is published, it throws an error A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.

  • [InvalidOperationException: A circular reference was detected while serializing an object of type 'System.Reflection.RuntimeModule'.]
  • System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +1856
  • System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat, MemberInfo currentMember) +266
  • System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat) +668

The controller look like this

public ActionResult
    SaveManualMeter(string clientId, ManualMeter manualMeter)

Does someone knows what the problem is?

The structure of manualmeter looks like

 [JsonObject(IsReference = false)]
    public class ManualMeter
    {
        public int Id { get; set; }      
        public string Ean { get; set; }        
        public PeriodType Period { get; set; }        
        public string TagTable { get; set; }
        public string TagTableId { get; set; }
        public int Overflow { get; set; }
        public string TZ { get; set; }

        public DataLogger DataLogger { get; set; }

        public Tag Tag { get; set; }

        public List<ManualMeterAction> ManualMeterActions { get; set; }       
    }

 public class ManualMeterAction
    {
        public int Id { get; set; }
        public ManualMeterActionType Type { get; set; }

        //navigation properties
        public int ManualMeterId { get; set; }
        public int UserId { get; set; }
    }

raw json object that is posted seems ok

{"clientId":"11","manualMeter":{"Tag":{"Name":"22","Unit":"Unknown","TagType":"0","Content":"1"},"Id":0,"Ean":"","ManualMeterActions":[],"Period":"0","Overflow":"22","TZ":"Europe/Brussels"}}

raw json image

1 Answers1

0

What I think is the problem is the table relationship with DataLogger and Tag. To get around this create a viewmodel that will remove any relationship like so:

public class ManualMeterViewModel { 
    public string Ean { get; set; } 
    public string TagTable { get; set; }
    public string TagTableId { get; set; } 
    public int Overflow { get; set; } 
    public string TZ { get; set; } 
    public List<ManualMeterAction> ManualMeterActions { get; set; } 
}

And the use is in your controller like this:

public ActionResult
    SaveManualMeter(string clientId, ManualMeterViewModel manualMeter){

var realmanualMeter = new ManualMeter(){
        Ean = manualMeter.Ean,
        TagTable = manualMeter.TagTable
        … 
    };
 }

Hope this helps.

Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
  • Thanks for your reply mark. Do you have a clue why it works locally and not when deployed to IIS? The thing is, we use a lot of javascript based on the manualmetermodel and your suggested solution requires a lot of changes. – Gunther Scheyltjens May 20 '16 at 10:53
  • Am not sure. But what am sure about though is that the error has nothing to do with IIS. – Mark Omoniyi Adesina May 20 '16 at 11:13
  • I am also getting this error. And i think its something to do with same field name found in JSON object. – Ali Exalter Feb 06 '17 at 12:32