I have this function:
$.ajax({
type:"post",
url:"/Home2/test01",
data: procesoData,
succes: function(datos){
$("#DatosRespuesta").html(datos);
}
);
where processData corresponds to a list of data:
var procesoData = { 'items':[ ] }
My question is how can I work with them (receive the parameter as a list) in an MVC 5 controller in C#? Any help would be nice, thanks.
MVC does the binding according to the parameter names and type, so to receive the elements of an array as per the example in your Home2 controller :
Here it depends on the type of elements that are in
items
, in the simplest case if it were a set of user stringsList<string>
.You are probably sending objects with more properties, in that case you would have to create a class in C# that follows the structure of the JavaScript objects so that MVC deserializes them in it. For example if on the client side you have
So in C# you could create a class
and in the action signature use
List<Usuario> items
On the other hand, if the object that is being sent has more properties than just
items
it, it would be advisable to create a specific model to receive everythingthe data would also be mapped automatically by the name of the properties and you would use this model