0

Im using SlickGrid and was tying to add data from grid to the DB when the user press the Enter key. It is sendig and JSON object (e.g. {"name":"value"}) and I need it as an array since im using type 'System.Collections.Generic.List'. Is its possible, in the controller , change it to a JSON array ? The data is obtained from the grid itself. Thanks in advance!

I've seen some similar situations but Im still gettint the error :

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[DAL.SlickGridTest]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.

this is my action public ActionResult EditGridEnter(string mydata) and this is my jquery $.post

grid.onKeyDown.subscribe(function(e) {
  var keyPressed = event.keyCode || event.which;
  if (keyPressed == 13) {
    var myJSON = JSON.stringify(item);
    $.post("/SlickGridTest/EditGridEnter", $("input[name=mydata]").val(myJSON));
  }
});

This is what comes from item :

var idData = jsonResult[key].id + 1;
var item = { "id": idData, "t_nome": "", "t_prof": "", "t_data": "", "t_morada": "", "t_percCompleto": "" };

all data comes from the grid in slickgrid

This is how I deserialize the object:

var slcgrd= JsonConvert.DeserializeObject<List<SlickGridTest>>(mydata);

I expect to get values as an array (e.g. [1,2,3]) and im getting as object ( Object {id: 43, t_nome: "name name", t_prof: "prof", …} )

daaavs
  • 35
  • 7
  • what's in `item`? – GrafiCode May 24 '19 at 09:50
  • Your MVC action takes a string, so your question is missing information concerning ***how*** you are attempting to deserialize that string to your desired type, and what that type is. You also don't describe what you're sending (i.e. the value of `item` in the JS code) – spender May 24 '19 at 09:52
  • @GrafiCodeStudio updated the code, you can see it now! – daaavs May 24 '19 at 09:54
  • `var array = Object.keys(item).map(function(key) { return [Number(key), item[key]]; });` as seen on this post: https://stackoverflow.com/questions/38824349/how-to-convert-an-object-to-an-array-of-key-value-pairs-in-javascript – GrafiCode May 24 '19 at 09:54
  • @spender udpate with the deserialized ! sorry about that – daaavs May 24 '19 at 09:56
  • or, if you only need an array of values (without preserving the object's keys) just use: `var array = Object.values(item);` – GrafiCode May 24 '19 at 09:59
  • So is type `SlickGridTest` the same shape as that JS variable, `item`? – spender May 24 '19 at 10:03
  • @GrafiCodeStudio ok, now is in an array! My question now is : can the controller recieve an array as argument? Im kinda fresh to ASP.NET and self-learning :c – daaavs May 24 '19 at 10:06
  • @spender yes ! But the `SlickGridTest` is my DAL class, and `item` is where the data the user introduced in the grid goes. Then is passes and comes to the controller as JSON object ! But my `SlickGridTest` needs an array . ( if I get your question right) – daaavs May 24 '19 at 10:09
  • uhm, there's no `list` data type in javascript, but a simple array like that is basically a list... hope asp.net likes it – GrafiCode May 24 '19 at 10:09
  • @GrafiCodeStudio hum , the controller does not get the data ! it recieves `mydata Count = 0` :/ – daaavs May 24 '19 at 10:19
  • 1
    Isn't it then as simple as wrapping the item up in an array? `var myJSON = JSON.stringify( [item] );` ? – spender May 24 '19 at 11:47
  • 1
    ... or deserializing a single object instead of a list? `var slcgrd= JsonConvert.DeserializeObject(mydata);` ? – spender May 24 '19 at 11:49
  • @spender I've done deserializing a simple object instead of list! Didnt update yet because was at lunch break! But thanks! Even your first comment its pretty simple and more practice, since I dont need to change the controller! Put as answer so I can accept! and thanks a lot! – daaavs May 24 '19 at 13:07

1 Answers1

1

So, it appears you are trying to deserialize to a List<SlickGridTest>. When deserializing to a list, the deserializer expects to see a JSON array structure. As such you have two choices.

Either send an array with a single item:

var myJSON = JSON.stringify( [item] );

or change your controller deserialization to expect a single item instead:

var slcgrd= JsonConvert.DeserializeObject<SlickGridTest>(mydata);
spender
  • 117,338
  • 33
  • 229
  • 351