0

I'm trying to send a collection from a view to a controller, using ajax. First I complete the data in a javascript array, and then I try to pass it to the server:

 var funciones = $("#funcionesUsuario").data("kendoGrid").dataSource.data();
 var func = [];
 for (var i = 0; i < funciones.length; i++) {
 func.push({
 "Estado": 1,
 "FechaAlta": null,
 "UsuarioAlta": null,
 "FechaModificacion": null,
 "UsuarioModificacion": null,
 "BitConcilia": funciones[i].BitConcilia,
 "BitLectura": funciones[i].BitLectura,
 "BitSupervisa": funciones[i].BitSupervisa,
 "ConciliacionId": funciones[i].ConciliacionId,
 "UsuarioId": funciones[i].UsuarioId
 })
 }
 $.post(url, { funcionesUsuario: func })
 .done(function (data) {
 alert("good job");
 });

Then, since I'm sending data for 2 objects, my parameter is a IEnumerable of said object:

public void ActualizarFuncionesUsuario(IEnumerable<FuncionUsuario> funcionesUsuario)
 {
 //do something
 }

My problem is that the controller receives 2 objects, as I can see in the funcionesUsuario.count, but they are both empty.

I tried sending int, bool and other types of variables with success, so I suspect I'm doing something wrong regarding data binding.

Below I attached pictures of what I'm sending and what I'm receiving in the other side.

Picture of the js function Picture of the controller

This is the FuncionUsuario model: `

public class FuncionUsuario : AuditableEntity { 
 public int ConciliacionId { get; set; } 
 public int UsuarioId { get; set; } 
 public bool BitLectura { get; set; } 
 public bool BitConcilia { get; set; } 
 public bool BitSupervisa { get; set; } 
 public virtual Conciliacion Conciliacion { get; set; } 
 [Display(ResourceType = typeof(Global), Name = "Descripcion")] 
 [NotMapped] 
 public string Descripcion { get; set; } 
}
asked Jun 15, 2016 at 19:37
3
  • What does your FuncionUsuario model schema look like? Commented Jun 15, 2016 at 19:46
  • This is not a definitive answer, but there are a couple of similar questions where stringifying the data / providing an appropriate contentType did the trick. I would definitely try specifying contentType first, and then follow that up with stringifying only if necessary. stackoverflow.com/questions/10101896/…, stackoverflow.com/questions/14691977/… Commented Jun 15, 2016 at 19:57
  • @Nkosi thanks for the edit! I was going to do that but you beat me to it. Commented Jun 15, 2016 at 20:26

2 Answers 2

1

first construct you array as java script objects

var funciones = $("#funcionesUsuario").data("kendoGrid").dataSource.data();
var func = [];
for (var i = 0; i < funciones.length; i++) {
 func.push({
 Estado: 1,
 FechaAlta: null,
 UsuarioAlta: null,
 FechaModificacion: null,
 UsuarioModificacion: null,
 BitConcilia: funciones[i].BitConcilia,
 BitLectura: funciones[i].BitLectura,
 BitSupervisa: funciones[i].BitSupervisa,
 ConciliacionId: funciones[i].ConciliacionId,
 UsuarioId: funciones[i].UsuarioId
 })
}

Next, properly stringify them

var payload = JSON.stringify(func);

Then post it as JSON

$.ajax({
 url: url,
 type: "POST",
 contentType: "application/json",
 dataType: 'json'
 data: payload,
});
answered Jun 15, 2016 at 20:00

1 Comment

I can't explain why, but now it's working! I was so tired yesterday I probably left some unfinished code from some test, and today I tried again and I works like a charm. Thanks a lot.
1

Try these:

Specify json as the dataType

$.post(url, { funcionesUsuario: func })
 .done(function (data) {
 alert("good job");
}, 'json');

Use $.ajax instead: docs

$.ajax({
 url: url,
 data: JSON.stringify(func),
 contentType: "application/json",
 type: "POST"
});
answered Jun 15, 2016 at 19:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.