Possible bug in JSON processing back to server

I can create a JSON complex object with values and a list of objects for example:

[code]
public class Event
{
public int id {get; set;}
public string text { get; set; }
public DateTime start_date { get; set; }
public DateTime end_date { get; set; } // not going to really use this
public int site { get; set; }
public String sDateStart { get; set; }
public String sTimeStart { get; set; }
public DateTime DateStart {get;set;}
public DateTime TimeStart { get; set; }
public List shiftTypes { get; set; }
public Boolean IgnoreShiftTypes { get; set; }
public String sStartShiftTime { get; set; }
public String sEndShiftTime { get; set; }
public DateTime StartShiftTime { get; set; }
public DateTime EndShiftTime { get; set; }
public int MaxShiftsPerDay { get; set; }
public int MaxShiftsPerSubmit { get; set; }
public List shiftDates { get; set; }

   }[/code]

and when this is sent to the client it’s wrapped in the

var ev = scheduler.getEvent(scheduler.getState().lightbox_id);

However when these values are sent back to the server, specifically shiftTypes and shiftDates are missing. I could get these from the form values but I started to wrap all the data in json in the ev object. The simple values show up fine, the complex objects are missing.

Just to make sure I’m clear all the values including complex objects show up in the client. I can do a stringify and see the values. However on the server:

updatedEvent = context.Jobs.SingleOrDefault( ev => ev.id == updatedEvent.id);

The shiftTypes, shiftDates are missing all other values are present.

Sorry… my error casting to wrong object… please delete thread.

Actually there is still an issue here. I would like to know if there’s something I’m doing wrong. I am creating the JSON objects correctly and I can get the values on the client. So for example:

                var SelectedShifts= ev.shiftTypes;
                for (i=0;i<=SelectedShifts.length-1;i++){
                    var ss= SelectedShifts[i];
                    var key= ss.key;
                    var val= ss.value;
                    $('#ShiftPriority')
                            .append($('<option>', { val: key })
                            .text(val));
                }

works correctly.

However, when the JSON object unmolested is returned to server the variable exists but there’s no data. Could it be something wrong with how the JSON object is converted back to my class.

This is essentially what I’ve created:


 public class ShiftTypes {
                public int key {get; set;}
                public string value {get; set;}
                public int priority { get; set; }
         }

         public class ShiftDates
         {
             public int key { get; set; }
             public String StartDate { get; set; }
             public String EndDate { get; set; }
             public int priority { get; set; }
         }
        
        public class Event
        {
            public int id {get; set;}
            public string text { get; set; }
            public DateTime start_date { get; set; }           
            public DateTime end_date { get; set; } // not going to really use this
            public int site { get; set; }
            public String sDateStart { get; set; }
            public String sTest { get; set; }
            public String sTimeStart { get; set; }
            public DateTime DateStart {get;set;}
            public DateTime TimeStart { get; set; }
            public List<ShiftTypes> shiftTypes { get; set; }
            public Boolean IgnoreShiftTypes { get; set; }
            public String sStartShiftTime { get; set; }
            public String sEndShiftTime { get; set; }
            public DateTime StartShiftTime { get; set; }
            public DateTime EndShiftTime { get; set; }
            public int MaxShiftsPerDay { get; set; }
            public int MaxShiftsPerSubmit { get; set; }
            public List<ShiftDates> shiftDates { get; set; }
}

public ActionResult Save(Event updatedEvent, FormCollection formData)

Thanks in advance

I maybe wrong, but the ShiftDates and shiftTypes are complex objects, right ? They are converted to json array or json sub-structure, that is fine for data loading, but data saving can process correctly only simple parameters, it will not work for sub-arrays or sub-object ( scheduler’s code will convert all object to string at once, which will produce some useless data, that can’t be restored but to complex structure on the server side )

Ok. Thanks. Then the best way is to store this JSON object in a hidden value on my custom form and deserialize it.

It doesn’t seem that the FormCollection doesn’t contain the variables of the form if you do a custom form. What am I missing?

Just FYI in case anybody else tries this I found the easiest way is just to bypass the .endLightBox by and doing my own ajax post by doing something like this:

 $.ajax({
                        type: "POST",
                        url: "Save",
                        data: JSON.stringify(ev),
                        success: success,
                        dataType: "json",
                        contentType: "application/json"
                    });

                    scheduler.endLightbox(false, html("my_form"));

and on the server I created my own Action handler


 public ActionResult Save(Event updatedEvent)
{
....
}