var formData = $(self.parentRow).find('form').serializeArray(); var calendarAttachment = []; calendarAttachment[0] = { PKID: 10 }; calendarAttachment[1] = { PKID: 23 }; calendarAttachment[2] = { PKID: 199 }; formData.push({ name: 'CalendarAttachments', value: calendarAttachment }); $.post('/Calendar/Save', formData, function (json) {...} 

There is the following code in js. When transferring data to the controller does not want to receive values. And he does not want to receive the values ​​of this array. When transferring other data all the rules.

 public List<EventFileItem> CalendarAttachments { get; set; } 

Above is the property that contains the model.

 public class EventFileItem { public long PKID { get; set; } public string Description { get; set; } public long? CalendarId { get; set; } public string FilePath { get; set; } } 

Tried to make a PKID field and as a string, but nothing helped. HELP!

    1 answer 1

    The problem is that the serializeArray function serializeArray internal arrays that are not in the form data format. Data is sent as: enter image description here

    And should come in the following form:

    enter image description here

    To do this, these forms need to be serialized into a js-object. Go or add your function:

     $.fn.serializeObject = function() { var o = {}; var a = this.serializeArray(); $.each(a, function() { if (o[this.name] !== undefined) { if (!o[this.name].push) { o[this.name] = [o[this.name]]; } o[this.name].push(this.value || ''); } else { o[this.name] = this.value || ''; } }); return o; }; 

    And then use it

     var formData = $(self.parentRow).find('form').serializeArray(); var calendarAttachment = []; calendarAttachment[0] = { PKID: 10 }; calendarAttachment[1] = { PKID: 23 }; calendarAttachment[2] = { PKID: 199 }; formData['CalendarAttachments'] = calendarAttachment; $.post('/Calendar/Save', formData, function (json) {...}); 

    Or just use reduce for an array when sending data:

     $.post('/Calendar/Save', formData.reduce(function(previousValue, currentValue) { previousValue[currentValue.name] = currentValue.value; return previousValue; }, {}), function (json) {...})