ASP Net Core MVC Ajax Post

时间:2017-08-23 08:23:42

标签: javascript c# ajax asp.net-core asp.net-core-mvc

我有以下代码,它完全正常,直到我在js对象中添加最后一个属性

JS:

var serializableFilter = {};

serializableFilter.aosType = filter.aos.type;
serializableFilter.aosText = filter.aos.text;
serializableFilter.industries = filter.industries.getCommaSeperatedIDs();
serializableFilter.servicingBanks = filter.servicingBanks.getCommaSeperatedIDs();
serializableFilter.category = filter.categories.categoryID;
serializableFilter.yearEstablishedFrom = filter.yearEstablished.from;
serializableFilter.yearEstablishedTo = filter.yearEstablished.to;
serializableFilter.staffMin = filter.nbStaff.min;
serializableFilter.staffMax = filter.nbStaff.max;
//serializableFilter.includeBranches = filter.includeBranches;

$.ajax({
    url: url,
    type: "POST",
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: JSON.stringify(serializableFilter),
    success: function (result) {
        console.log(result);
    },
    error: function () {
        alert('error');
    }
});

C#:

[HttpPost]
public async Task<IEnumerable<FdxxxMap>> GetC([FromBody] Filter data)

型号:

public class Filter
{
    public string aosType { get; set; }
    public string aosText { get; set; }
    public string industries { get; set; }
    public string servicingBanks { get; set; }
    public int category { get; set; }
    public int yearEstablishedFrom { get; set; }
    public int yearEstablishedTo { get; set; }
    public int staffMin { get; set; }
    public int staffMax { get; set; }
    public Boolean includeBranches { get; set; }
}

每当我添加boolean类型的最后一个属性时,数据模型就会传递为null

1 个答案:

答案 0 :(得分:0)

我想知道filter.includeBranches的类型是否真的是bool。 你为什么不试试这个:

POST /movies/_find HTTP/1.1
Accept: application/json
Content-Type: application/json
Content-Length: 168
Host: localhost:5984

{
    "selector": {
        "year": {"$gt": 2010}
    },
    "fields": ["_id", "_rev", "year", "title"],
    "sort": [{"year": "asc"}],
    "limit": 2,
    "skip": 0
}