【问题标题】:C# optional parameter not properly mapped with json data of Ajax(with same name of different types)C# 可选参数未正确映射到 Ajax 的 json 数据(具有不同类型的相同名称)
【发布时间】: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


    【解决方案1】:

    所有这些魔法都称为 MVC 模型绑定。 如果CustomerSelectorResult 在您的请求中不为空,则默认模型绑定将尝试从您的请求中构造它,并在您的请求中找到displayId

    MSDN Article

    请尝试此解决方案。这不是最好的,但它会解决你的问题。 创建从DefaultModelBinder派生的新类

    public class CustomerSelectorResultModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var data = base.BindModel(controllerContext, bindingContext);
    
            var model = data as CustomerSelectorResult;
            //check if CustomerSelectorResult's all properties are null except DisplayId.
            if(model != null &&
               model.Name == null &&
               model.RoleId == default(int) &&
               model.RoleName == null &&
               model.Address == null)
            {
                return null;
            }
    
            return data;
        }
    }
    

    并将此类应用于您的操作

    public JsonResult GetCusotmerByDisplayId(string displayId, string transportationModeName, int jagurRef, [ModelBinder(typeof(CustomerSelectorResultModelBinder))]CustomerSelectorResult alternatePickUpCarrier = null)
         {
               //my logic
         }
    

    【讨论】:

    • 在我的情况下,我们不能将可选参数值设为 null 吗?
    • @ManirajSS 请看我的解决方案。有什么不对就说吧。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多