ASP.net MVC JsonValueProvider字节类型问题

时间:2012-07-16 17:18:01

标签: c# asp.net-mvc-3 value-provider

我将JSON发布到控制器方法,看起来好像ASP.Net MVC 3中包含的JsonValueProvider在尝试将JSON数字转换为字节类型时被破坏。

基本上发生的事情是,任何需要转换为字节的非null值都为null,我的模型状态有错误,没有错误消息。如返回JSON中的问题列表所示。 dnbscore和region有错误,但评级没有。返回的数据将dnbscore和region设置为null,即使在传递JSON时,这些字段也具有值。我可以使用自定义模型绑定器来处理这个问题,但我想知道是否有一种不同的方法可以让JsonValueProider像我期望的那样工作。

这是我想要构建的对象:

public class APICustomerDetailsDTO
{
    public int customerid { get; set; }

    [StringLength(30)]
    public string customername { get; set; }
    public decimal? globalcredit { get; set; }
    [Range(0,5)]
    public byte? rating { get; set; }
    [Range(0, 100)]
    public byte? dnbscore { get; set; }
    [Range(0, 8)]
    public byte? region { get; set; }
    [Required]
    public bool isnotactive { get; set; }
    public string salesperson { get; set; }
    [StringLength(30)]
    public string newmill_venderid { get; set; }
    [StringLength(30)]
    public string newmill_supplierid { get; set; }
    [StringLength(30)]
    public string edi_receiverid { get; set; }
    [Required]
    public bool edi_invoice { get; set; }
    [StringLength(15)]
    public string bill_code { get; set; }
}

这是我在帖子请求中发送的JSON:

{"bill_code":"good","customerid":50,"customername":"Ted","dnbscore":80,"edi_invoice":false,"edi_receiverid":null,"globalcredit":null,"isnotactive":false,"newmill_supplierid":null,"newmill_venderid":null,"rating":null,"region":0,"salesperson":null}

检查模型状态的方法的一部分:

        if (!ModelState.IsValid)
        {
            var issues = ModelState.Where(m => m.Value.Errors.Any())
                                   .Select((m)=> new {field = m.Key, error = m.Value.Errors.FirstOrDefault().ErrorMessage})
                                   .ToArray();

            var result = new
            {
                result = "Failure",
                message = "Invalid data received. See issues for details.",
                issues = issues,
                data = cust
            };

返回JSON:

{"result":"Failure","message":"Invalid data received. See issues for details.","issues":[{"field":"dnbscore","error":""},{"field":"region","error":""}],"data":{"customerid":50,"customername":"Ted","globalcredit":null,"rating":null,"dnbscore":null,"region":null,"isnotactive":false,"salesperson":null,"newmill_venderid":null,"newmill_supplierid":null,"edi_receiverid":null,"edi_invoice":false,"bill_code":"good"}}

为了完整起见,我在这里解决了这个问题:

根据Darin提供的信息,我意识到这是一个比字节更大的问题?到int?转换。对于默认绑定器提供的任何非int转换,这将是一个问题。因此,我制作了一个自定义绑定器,它应该处理字节,小数等数字。见下文:

public class APICustomerDetailsDTOBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        APICustomerDetailsDTO model = (APICustomerDetailsDTO)bindingContext.Model ??
            (APICustomerDetailsDTO)DependencyResolver.Current.GetService(typeof(APICustomerDetailsDTO));

        bool hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);
        string searchPrefix = (hasPrefix) ? bindingContext.ModelName + "." : "";

        int customerid = 0;
        int.TryParse(GetValue(bindingContext, searchPrefix, "customerid"), out customerid);
        model.customerid = customerid;

        string customername = GetValue(bindingContext, searchPrefix, "customername");
        if (!String.IsNullOrEmpty(customername))
        { model.customername = customername; }
        else
        { model.customername = null; }

        decimal globalcredit;
        if (decimal.TryParse(GetValue(bindingContext, searchPrefix, "globalcredit"), out globalcredit))
        { model.globalcredit = globalcredit; }
        else
        { model.globalcredit = null; }

        byte rating;
        if (byte.TryParse(GetValue(bindingContext, searchPrefix, "rating"), out rating))
        { model.rating = rating; }
        else
        { model.rating = null; }

        byte dnbscore;
        if (byte.TryParse(GetValue(bindingContext, searchPrefix, "dnbscore"), out dnbscore))
        { model.dnbscore = dnbscore; }
        else
        { model.dnbscore = null; }

        byte region;
        if (byte.TryParse(GetValue(bindingContext, searchPrefix, "region"), out region))
        { model.region = region; }
        else
        { model.region = null; }

        bool isnotactive;
        if (bool.TryParse(GetValue(bindingContext, searchPrefix, "isnotactive"), out isnotactive))
        { model.isnotactive = isnotactive; }
        else
        { model.isnotactive = false; }

        string salesperson = GetValue(bindingContext, searchPrefix, "salesperson");
        if (!String.IsNullOrEmpty(salesperson))
        { model.salesperson = salesperson; }
        else
        { model.salesperson = null; }

        string newmill_venderid = GetValue(bindingContext, searchPrefix, "newmill_venderid");
        if (!String.IsNullOrEmpty(newmill_venderid))
        { model.newmill_venderid = newmill_venderid; }
        else
        { model.newmill_venderid = null; }

        string newmill_supplierid = GetValue(bindingContext, searchPrefix, "newmill_supplierid");
        if (!String.IsNullOrEmpty(newmill_supplierid))
        { model.newmill_supplierid = newmill_supplierid; }
        else
        { model.newmill_supplierid = null; }

        string edi_receiverid = GetValue(bindingContext, searchPrefix, "edi_receiverid");
        if (!String.IsNullOrEmpty(edi_receiverid))
        { model.edi_receiverid = edi_receiverid; }
        else
        { model.edi_receiverid = null; }


        bool edi_invoice;
        if (bool.TryParse(GetValue(bindingContext, searchPrefix, "edi_invoice"), out edi_invoice))
        { model.edi_invoice = edi_invoice; }
        else
        { model.edi_invoice = false; }

        model.bill_code = GetValue(bindingContext, searchPrefix, "bill_code");

        return model;
    }

    private string GetValue(ModelBindingContext context, string prefix, string key)
    {
        ValueProviderResult vpr = context.ValueProvider.GetValue(prefix + key);
        return vpr == null ? null : vpr.AttemptedValue;
    }

    private bool GetCheckedValue(ModelBindingContext context, string prefix, string key)
    {
        bool result = false;
        ValueProviderResult vpr = context.ValueProvider.GetValue(prefix + key);
        if (vpr != null)
        {
            result = (bool)vpr.ConvertTo(typeof(bool));
        }
        return result;
    }
}

2 个答案:

答案 0 :(得分:1)

您可以将它们作为字符串发送:

{ 
    "bill_code": "good", 
    "customerid": 50, 
    "customername": "Ted", 
    "dnbscore": "80", 
    "edi_invoice": false, 
    "edi_receiverid": null, 
    "globalcredit": null, 
    "isnotactive": false, 
    "newmill_supplierid": null, 
    "newmill_venderid": null, 
    "rating": null, 
    "region": "0", 
    "salesperson": null 
}

如果您对更多详细信息感兴趣,可以查看following post

答案 1 :(得分:0)

我的自制解决方案是创建一个javascript函数,确保所有数值都用它们周围的引号序列化,如下所示

$.toJson = function (data) {
    return JSON.stringify(param, function (k, v) { return typeof v === 'number' ? v.toString() : v; });
};