Array of objects after stringifying is not passed to controller action and the parameter of action (the model) is null
.
I have tried many solutions given to this problem in StackOverflow
but none of these solution solved my problem. I provided the links which I tried:
Pass array to mvc Action via AJAX
pass array in javascript object to mvc controller
Here is my code
JavaScript Code
$("#FormSubmit").click(function () {
var datalist = [];
$("#MarksTable tbody tr").each(function () {
var obj = {};
obj.StudentID = 1;
obj.ExamTimeTableID = 1;
obj.ClassActivity = $(this).find("td:eq(5) input").val();
obj.IsPresent = $(this).find("td:eq(7) input").val();
obj.Assignment = $(this).find("td:eq(6) input").val();
obj.MidTerm = $(this).find("td:eq(3) input").val();
obj.Final = $(this).find("td:eq(4) input").val();
datalist.push(obj);
});
$.ajax({
url: "@Url.Action("InsertData", "Examination")",
type: "POST",
cache: false,
contentType: "application/json;charset=utf-8",
dataType: "json",
data: JSON.stringify(datalist),
success: function (result) {
alert(result);
},
error: function (errormessage) {
alert(errormessage.responseText);
}
});
});
Action of Controller:
[HttpPost]
public JsonResult InsertData([FromBody] List<StudentMarksList> obj)
{
DynamicParameters param = new DynamicParameters();
.
.
.
return Json(param.Get<string>("msg"));
}
The Model:
public class StudentMarksList
{
public int StudentID { get; set; }
public int ExamTimeTableID { get; set; }
public int ClassActivity { get; set; }
public int IsPresent { get; set; }
public int Assignment { get; set; }
public int MidTerm { get; set; }
public int Final { get; set; }
}
Request Payload:
[,…]
0: {StudentID: 1, ExamTimeTableID: 1, ClassActivity: 3, IsPresent: 0, Assignment: 2, MidTerm: 5, Final: 4}
Assignment: 2
ClassActivity: 3
ExamTimeTableID: 1
Final: 4
IsPresent: 0
MidTerm: 5
StudentID: 1
Request Payload source:
[{"StudentID":1,"ExamTimeTableID":1,"ClassActivity":3,"IsPresent":0,"Assignment":2,"MidTerm":5,"Final":4}]
The obj
should contains the passed objects but it is null
.
Any Help???