猿问

对象引用 Null - 将 jQuery 数组传递给 C# 列表

我有一个简单的问题,但它变得复杂。我正在尝试使用jQuery和在后端传递一个对象数组,我正在使用它C#来获取列表。所以这是我到目前为止所尝试的:


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);

        }

    });

});

所以这个想法是:有一个表,每一行都有一个CheckBox与之关联的。每当用户检查一行或多行时,它应该在 an 中包含行数据Array并迭代,然后在Ajax调用中传递给 C# 控制器。这是C#代码部分:


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>";

      }

   }


不幸的是,每次调用C#控制器时我都会收到此错误,尽管我很确定我在做正确的事情 - Object reference not set to an instance of an object。我在这里错过了什么?


烙印99
浏览 180回答 2
2回答

浮云间

您不能发布包含的对象FormData- 您需要发送实际FormData对象。此外,您name与发布到的模型不匹配,该模型是一个集合,而不是包含集合的对象。假设DailyStrikeOffBO包含 properties empno,Name,Des` 等,那么您需要将名称/值对附加为formData.append('[' + i + '].empno', row.empno);formData.append('[' + i + '].Name', row.Name);formData.append('[' + i + '].Des', row.Des);.... // etc然后将ajax选项修改为$.ajax({&nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; url: url,&nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; data: formData , // modify&nbsp; &nbsp; contentType: false,&nbsp; &nbsp; processData: false,&nbsp; &nbsp; async: false,&nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; alert("Updated. - "+data);&nbsp; &nbsp; }});

慕慕森

for (a = 0; a < rowindexes.length; a++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var row = $('#jqxgrid').jqxGrid('getrowdata', rowindexes[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var model = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; empno : row.empno,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Name: row.Name,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Des: row.Des,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dept: row.Dept,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Section: row.Section,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Emp_type:&nbsp; row.Emp_type,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Diff: row.Diff,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; LateAtt: row.LateAtt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data.push(model);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;var modelString = JSON.stringify(data);&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; url: url,&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; data: modelString ,&nbsp; &nbsp; &nbsp; &nbsp; contentType: "application/json; charset=utf-8",&nbsp; &nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; success: function (data) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert("Updated. - "+data);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });而不是使用 FormData 尝试使用 json 对象。您通过 ajax ( data: JSON.stringify({ 'things': formData }), ) 传递数据的方式也是不正确的。试试上面的代码,让我知道它是怎么回事。
随时随地看视频慕课网APP
我要回答