Reputation: 7197
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
}
Upvotes: 1
Views: 182
Reputation: 247133
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),
...
Upvotes: 1
Reputation: 1438
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
,
Upvotes: 2