1

I am using VS2015, C# and trying to pass JSON array to MVC controller.

JSON string I am trying to send is (I've checked it's valid JSON):

[{"iUSER_KEY":"130000096","iUSNW_KEY":"160001926"},{"iUSER_KEY":"160000209","iUSNW_KEY":"160001779"}]

Error I am receiving is:

[ArgumentException: Invalid JSON primitive: keysList.]

On a client my code is:

$('.do-remove').click(function (e) {

      e.preventDefault();

  var keys = [];
  $("#tableUSNW tbody tr").each(function () {

    var row = $(this);
    var checked = $(row).find("td > input[id=inputChk]").is(":checked");

    if (checked === true) {

      var iUSER_KEY = $(row).attr("data-userkey");
      var iUSNW_KEY = $(row).attr("data-ref");
      var user = {iUSER_KEY: iUSER_KEY, iUSNW_KEY: iUSNW_KEY};
      keys.push(user);

      row.remove();
    }
  });

  keys = JSON.stringify(keys);

  $.ajax({
    type: "POST",
    url: '/newsinternal/UpdateUsnwRemove',
    traditional: true,
    contentType: "application/json; charset=utf-8",
    data: { keysList: keys },
    beforeSend: function () {
      $('#loader').show();
    },
    success: function (msg) {
      $('#loader').hide();          
    },
    error: function (xhr, ajaxOptions, thrownError) {
      $('#loader').hide();
      alert(xhr.responseText);
    }
  });
});

On the server (MVC controller method):

[HttpPost]
public void UpdateUsnwRemove(List<DTO_CAUSNW> keysList)
{
  using (iDatabase baza = new iDatabase(iPUURE.Web.Configuration.GetConnectionString()))
  {
    CommDB db = new CommDB(baza, false);
    foreach (var item in keysList)
    {
      DTO_CAUSNW usnw = new DTO_CAUSNW();          
      usnw.cUSNW_STA = "9";
      iQuery qDelete = db.LoadInsertCAUSNW_BASE(usnw, "4");  // 1 = select, 2 = insert, 3 = update 4 = delete
    }
  }
}

And class DTO_CAUSNW:

public class DTO_CAUSNW
{
    public int? iUSNW_KEY { get; set; } //user_news key 
    public string cUSNW_STA { get; set; } //status: 1=enable, 2/null=disable, 9=erased 
    public string cUSNW_SRT { get; set; } //sort: 1=internal news
    public DateTime? dUSNW_DAT { get; set; } //date of changed status

    public int? iNEWS_KEY { get; set; } //news key
    public int? iUSER_KEY { get; set; } //user key 

    public DateTime? dUSNW_DSI { get; set; } //time of signature
    public DateTime? dUSNW_DAU { get; set; } //time of user changed  
    public string cUSNW_STU { get; set; } //user status: 1=confirm, 2/null=not confirm
    public int? iUSNW_CPU { get; set; } //counter of postponed
    public string cUSNW_COM { get; set; } //comment
    public string cUSNW_NTO { get; set; } //note

    public DTO_CANEWS oNEWS { get; set; } //the news
    public DTO_BAUSER oUSER { get; set; } //the user
}
1
  • change the variable keys to keysList... and set data : keysList in your ajax call Commented Dec 9, 2016 at 14:44

2 Answers 2

2

when you send data: { keysList: keys } in you ajax post it means server must have an object whith keyslist property.

just change data: { keysList: keys } to data: keys,

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for answer. Is it possible to add some other parameters, let's say two more integers in method call?
@FrenkyB you mean somehing like UpdateUsnwRemove(List<DTO_CAUSNW> keysList,int param1,int param2) ?
Yes, like that.
@FrenkyB sure possible. than your data would be like data: { keysList: keys, param1:1, param2:2 } but remember parameter name would be important in this case and it's case sensitive
1

create your payload

$('.do-remove').click(function (e) {

  e.preventDefault();

  var keys = [];
  $("#tableUSNW tbody tr").each(function () {

    var row = $(this);
    var checked = $(row).find("td > input[id=inputChk]").is(":checked");

    if (checked === true) {

      var iUSER_KEY = $(row).attr("data-userkey");
      var iUSNW_KEY = $(row).attr("data-ref");
      var user = {iUSER_KEY: iUSER_KEY, iUSNW_KEY: iUSNW_KEY};
      keys.push(user);

      row.remove();
    }
  });


  var model = { keysList: keys };
  //..other code

and in the ajax stringyfy the whole thing.

...
data: JSON.stringify(model),
...

3 Comments

Thanks for answer. What if there were several parameters, let's say two more integers?
include them when creating the payload. var model = { keysList: keys, param1: 1, param2: 2 };
you should consider updating the action to take a complex object model that has the parameters as properties as you can only use one parameter from the body of the request

Your Answer

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.