0

我正在尝试向 .NET MVC 控制器发出 Ajax 请求。当我在控制器代码中设置断点时,我注意到数据为空。我希望它有数据。

ExtJS 代码:

var paramsList = [];
console.log(paramsList);
for (var i = 0; i < dropData.records.length; i++) {
    if (dropData.records[i].data.Conflicts.indexOf('This Camp' == -1)) {
        //add this cabin to the parameters object
        //idCampEventCabin = 0 tells the server to insert a new CampEventCabin
        paramsList.push({ idCampEventCabin: 0, Abbreviation: dropData.records[i].data.Abbreviation, Accommodations: dropData.records[i].data.Accommodations, Capacity: dropData.records[i].data.Capacity, idCabin: dropData.records[i].data.idCabin, Level: dropData.records[i].data.Level, Name: dropData.records[i].data.Name, Number: dropData.records[i].data.Number, Type: dropData.records[i].data.Type, idCampEvent: form.getIdCampEvent() });
    }
}

console.log(paramsList);
console.log(Ext.JSON.encode(paramsList));
if (params.length > 0) {
    //post 
    Ext.Ajax.request({
        url: '/CampEvent/UpsertCampEventCabins',
        method: 'POST',
        params: Ext.JSON.encode(paramsList),
        success: function (response, request) {
            var res = Ext.JSON.decode(response.responseText)
            if (res.success == true) {
                console.log('success');
                thisController.refreshCampEventCabinsForm(form);
            }
            else {
                Ext.Msg.alert('Add CampEventCabins Failed', res.message);
            }
        },
        failure: function (response, opts) {
            Ext.Msg.alert('Error', '<p>There was an error trying to save the record:</p><br /><p>The status code was: ' + response.status + '</p><br/><p>The error occurred in the function onMainTabCampEventCabinsFormRender()<p>');
        }


    });
}

MVC 控制器代码:

[HttpPost]
public JsonResult UpsertCampEventCabins(IList<ViewModels.CampEventCabin> model)
{
    //a breakpoint here shows that "model" is always null
    return Json(repo.UpsertCampEventCabins(model), JsonRequestBehavior.AllowGet);
}

“console.log(Ext.JSON.encode(paramsList));”这一行 产生这个结果:

[{"idCampEventCabin":0,"Abbreviation":"BH","Accommodations":null,"Capacity":10,"idCabin":2,"Level":"","Name":"Blackhawk","Number":"E2","Type":"Boys","idCampEvent":2},{"idCampEventCabin":0,"Abbreviation":"CH","Accommodations":null,"Capacity":9,"idCabin":3,"Level":"","Name":"Chinook","Number":"D1","Type":"Girls","idCampEvent":2}] 

我注意到的一件奇怪的事情是,当我使用 Chrome 的开发人员工具检查表单数据时,JSON 字符串的末尾有一个额外的冒号。像这样:

[{"idCampEventCabin":0,"Abbreviation":"BH","Accommodations":null,"Capacity":10,"idCabin":2,"Level":"","Name":"Blackhawk","Number":"E2","Type":"Boys","idCampEvent":2},{"idCampEventCabin":0,"Abbreviation":"CH","Accommodations":null,"Capacity":9,"idCabin":3,"Level":"","Name":"Chinook","Number":"D1","Type":"Girls","idCampEvent":2}]:

我希望控制器接受 JSON 数据作为 CampEventCabin 的一些集合。我试过“List”、“IList”、“IEnumerable”无济于事。我也尝试在不编码的情况下传递 params 对象。

我究竟做错了什么?

4

1 回答 1

0

您的代码似乎只是好的。只需检查控制器流程。作为 ExtJS 的一部分,根据我的理解,您编写的所有代码都是正确的。

试一试 public JsonResult UpsertCampEventCabins(String model)

然后使用 json 解析器在控制器端进行操作。

谢谢

于 2013-04-18T06:13:43.107 回答