0

我知道有类似的问题,但我的只是不同,足以让我问。

我正在尝试通过 ajax 将复杂对象发送到 .net MVC 后端。

接听电话是这样的:

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult SaveLenderMemberSettings(List<AssignmentFeeRule> AssignmentFeeRules)
{
    return Json(AssignmentFeeRules);
}

....

public class AssignmentFeeRule
{
    public string Currency { get; set; }
    public DateTime CutOffTimeBuy { get; set; }
    public DateTime CutOffTimeSell { get; set; }
    public int MinimumDaysToCloseBuy { get; set; }
    public int MinimumDaysToCloseSell { get; set; }
    public int OffsetDaysBuy { get; set; }
    public int OffsetDaysSell { get; set; }
}

调用被正确调用,但是当我尝试发送对象时,它被发送如下:

AssignmentFeeRules[0][Currency]:AZN
AssignmentFeeRules[0][MinimumDaysToCloseBuy]:2
AssignmentFeeRules[0][MinimumDaysToCloseSell]:2
AssignmentFeeRules[0][OffsetDaysSell]:2
AssignmentFeeRules[0][OffsetDaysBuy]:2
AssignmentFeeRules[0][CutOffTimeBuy]:01:00
AssignmentFeeRules[0][CutOffTimeSell]:14:01

由于此代码:

var assignmentFeeRule = {
    Currency:$(tds[0]).find('select, input').val(),
    MinimumDaysToCloseBuy:$(tds[1]).find('select, input').val(),
    MinimumDaysToCloseSel":$(tds[2]).find('select, input').val(),
    OffsetDaysSell:$(tds[3]).find('select, input').val(),
    OffsetDaysBuy:$(tds[4]).find('select, input').val(),
    CutOffTimeBuy:$(tds[5]).find('select, input').val(),
    CutOffTimeSell:$(tds[6]).find('select, input').val()
};

assignmentFeeRules[rowCount - 1] = assignmentFeeRule

var data = {
AssignmentFeeRules : assignmentFeeRules
};

$.ajax({
    url: url,
    method: "POST",
    data: data
}).done(function (data) {
    console.dir(data);
}).fail(function (error) {
    console.dir(error);
});

但我发现 MVC 需要它是这样的:

AssignmentFeeRules[0].Currency:AZN
AssignmentFeeRules[0].MinimumDaysToCloseBuy:2
AssignmentFeeRules[0].MinimumDaysToCloseSell:2
AssignmentFeeRules[0].OffsetDaysSell:2
AssignmentFeeRules[0].OffsetDaysBuy:2
AssignmentFeeRules[0].CutOffTimeBuy:01:00
AssignmentFeeRules[0].CutOffTimeSell:14:01

到目前为止,我对答案的所有搜索都没有找到任何东西。

所以基本上,底线是,我如何从关联数组中获取 json 对象?

4

0 回答 0