I've a simple issue but it got complicated. I am trying to pass an array of object using jQuery and in the back-end, I am using C# to get the list. So this is what I've tried so far:
jQuery:
$('#btnStrickOff').on('click', function () {
var formData = new FormData();
debugger;
var rowindexes = $('#jqxgrid').jqxGrid('getselectedrowindexes');
for (var i = 0; i < rowindexes.length; i++) {
var row = $('#jqxgrid').jqxGrid('getrowdata', rowindexes[i]);
formData.append('strData[' + i + '].empno', row.empno);
formData.append('strData[' + i + '].Name', row.Name);
formData.append('strData[' + i + '].Des', row.Des);
formData.append('strData[' + i + '].Dept', row.Dept);
formData.append('strData[' + i + '].Section', row.Section);
formData.append('strData[' + i + '].Emp_type', row.Emp_type);
formData.append('strData[' + i + '].LateAtt', row.LateAtt);
formData.append('strData[' + i + '].Diff', row.Diff);
}
var url = '@Url.Action("InsertStrikeOff")';
debugger;
$.ajax({
type: 'POST',
url: url,
dataType: 'json',
data: JSON.stringify({ 'things': formData }),
contentType: false,
processData: false,
async: false,
success: function (data) {
alert("Updated. - "+data);
}
});
});
So the idea is: There is a table and each row has a CheckBox associated with it. Whenever user checks a row or multiple, it should have the row data in an Array and iterate through, then passes to the C# controller in the Ajax call. Here is the C# code section:
C#:
public JsonResult InsertStrikeOff(List<DailyStrikeOffBO> things)
{
DateTime strikeDate = DateTime.Now;
var value = (dynamic)null;
foreach (var item in things)
{
bool chk = Facede.StrikeOff.CheckStrikeOff(item.empno);
if (chk == false)
{
bool aStrikeOffBo = Facede.StrikeOff.InserstrikeOffLst2(item.empno, item.Name, item.LateAtt, strikeDate, item.remarks);
value = "<div style='color:green;'>Striked-off request sent!</div>";
}
else
{
value = "<div style='color:red;'>Already striked off!</div>";
}
}
return Json(value, JsonRequestBehavior.AllowGet);
}
Unfortunately, I am getting this error every time when it calls the C# controller though I am quite sure I am doing the right thing - Object reference not set to an instance of an object. Anything that I missed here?
Update 1: Model
public class DailyStrikeOffBO
{
public string empno { get; set; }
public string Name { get; set; }
public string Des { get; set; }
public string Dept { get; set; }
public string Section { get; set; }
public string Emp_type { get; set; }
public string Diff { get; set; }
public string LateAtt { get; set; }
}
Update 2:

FormDataand stitch together the string of array of objects.