mvc jquery ajax传递数, areaIds是个int数组。后台action用list<int>接收。当我想传空值时,先用null传递,结果action收到的AreaIds竟然含有一个元素0,非常诡异,有空再查找具体原来。后来想传空时不再用Null,改成[],这样就ok了。很奇怪
function refreshStoreHouseDatas(marketId,areaIds,selectedValues) {
var data = {
marketId: marketId,
areaIds: areaIds
};
var url = "@Url.Action("LoadStoreHousesByAreaIds", "CommonAjax")";
$.post(url, data, function(result) {
$("#StoreHouseIds").data("kendoMultiSelect").setDataSource(result);
$("#StoreHouseIds").data("kendoMultiSelect").enable(true);
if (selectedValues!=null) {
$("#StoreHouseIds").data("kendoMultiSelect").value(selectedValues);
}
});
};
action
[HttpPost]
public ActionResult LoadStoreHousesByAreaIds(LoadStoreHousesByAreaIdsRequest request)
{
List<StoreHouse> storeHousesList;
if (request.AreaIds.Any())
{
storeHousesList = _commonService.GetEquipmentStoreHouses(request.AreaIds);
}
else
{
request.AreaIds = _commonService.ConvertToLeafChildAreas(new List<int>{ request.MarketId}).Select(m=>m.Id).ToList();
storeHousesList = _commonService.GetEquipmentStoreHouses(request.AreaIds);
}
var storeHouses = storeHousesList.Select(x => new SelectListItem
{
Value = x.Id.ToString(),
Text = x.Name
}).ToList(); return Json(storeHouses, JsonRequestBehavior.AllowGet);
}