0

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:

Sample Image

2
  • Note: I can see the data iterated in the JavaScript loop using browser console. Commented Sep 8, 2018 at 9:11
  • I'd simply skip the FormData and stitch together the string of array of objects. Commented Sep 8, 2018 at 9:25

2 Answers 2

1

You cannot post an object containing FormData - you need to send the actual FormData object. In addition your name do not match the model you are posting to, which is a collection, not an object containing a collection.

Assuming DailyStrikeOffBO contains properties empno, Name,Des` etc, then you need to append the name/value pairs as

formData.append('[' + i + '].empno', row.empno);
formData.append('[' + i + '].Name', row.Name);
formData.append('[' + i + '].Des', row.Des);
.... // etc

and then modify the ajax option to

$.ajax({
    type: 'POST',
    url: url,
    dataType: 'json',
    data: formData , // modify
    contentType: false,
    processData: false,
    async: false,
    success: function (data) {
        alert("Updated. - "+data);
    }
});
Sign up to request clarification or add additional context in comments.

6 Comments

I've already tried that too @Stephen Muecke. But still the same - Will try it again and let you know.
If its not working, then you need to show us your DailyStrikeOffBO model
The model is fine. The code in my answer will work fine. What problems are you still having?
I am still getting the error @Stephen Muecke. See the sample image.
Not possible if you made the changes I noted. But that image shows a model which is StrikeOffNew, not DailyStrikeOffBO that the data is based on.
|
0
for (a = 0; a < rowindexes.length; a++) {
            var row = $('#jqxgrid').jqxGrid('getrowdata', rowindexes[i]);
                var model = {
                    empno : row.empno,
                    Name: row.Name,                    
                    Des: row.Des,
                    Dept: row.Dept,
                    Section: row.Section,
                    Emp_type:  row.Emp_type,
                    Diff: row.Diff,
                    LateAtt: row.LateAtt
                };

                data.push(model);
            }   
var modelString = JSON.stringify(data);
    $.ajax({
        type: 'POST',
        url: url,
        dataType: 'json',
        data: modelString ,
        contentType: "application/json; charset=utf-8",
        processData: false,       
        success: function (data) {
            alert("Updated. - "+data);
        }
    });

Instead of using FormData try using json object. The way you are passing data thru ajax ( data: JSON.stringify({ 'things': formData }), ) that is also not correct.

Try the above code, let me know how it goes.

Comments

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.