【发布时间】:2015-01-05 16:58:24
【问题描述】:
ajax的json数据参数与c#方法的可选参数不匹配
$.ajax({
url: '/CusotmerManagement/Cusotmer/GetCusotmerByDisplayId/',
dataType: 'json',
data: {
displayId: selectedCustomerRow.DisplayId,
transportationModeName: selectedCustomerRow.TransportationModeName,
jagurRef: jaguarDetails
},
contentType: "application/json; charset=utf-8",
success: function (data) {
//some code
}
});
在上面的 ajax 调用中,我省略了可选参数(类 CustomerSelectorResult),并且我重要地传递了所有其他参数 displayId(主要客户的)。因为我没有次要参数客户我省略了 CustomerSelectorResult(注意:CustomerSelectorResult 也有 DisplayId)
C#方法
public JsonResult GetCusotmerByDisplayId(string displayId, string transportationModeName, int jagurRef, CustomerSelectorResult alternatePickUpCarrier = null)
{
//my logic
}
但在 c# 方法中,即使 CustomerSelectorResult 是可选参数,但存在于 CustomerSelectorResult 中的 displayId 和 DisplayId 具有相同的值。
CustomerSelectorResult 类
public class CustomerSelectorResult
{
public string Name { get; set; }
public string DisplayId { get; set; }//note display id within CustomerSelectorResult
public int RoleId { get; set; }
public string RoleName { get; set; }
public AddressResult Address { get; set; }
}
为什么可选参数的DisplayId取主参数(displayId)的值?
注意:我通过将值显式传递为 CustomerSelectorResult 克服了这个问题:null。
提前致谢。
【问题讨论】:
标签: c# ajax asp.net-mvc json optional-parameters