Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8242887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:19:16+00:00 2026-06-07T21:19:16+00:00

I am posting JSON to a controller method, and it appears as if the

  • 0

I am posting JSON to a controller method, and it appears as if the JsonValueProvider included with ASP.Net MVC 3 is broken when it tries to cast JSON numbers into the byte type.

Basically what is happening is that any non-null value that needs to be turned into a byte is null and my model state has errors with no error messages. As seen in the issue list in the return JSON. dnbscore and region have errors, but rating does not. The returned data has dnbscore and region as null even though when the JSON was passed, those fields had values. I can take care of this with a custom model binder, but I wonder if there is a different approach to make the JsonValueProider work as I expect it to.

This is the object that I am trying to build:

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; }
}

This is the JSON that I am sending in the post request:

{"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}

Part of my Method that checks model state:

        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
            };

Return 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"}}

For the sake of completeness here is what I did to get around this issue:

With the information provided by Darin, I realized this is a bigger issue than just byte? to int? conversion. It would be an issue for any non-int conversions provided by the default binder. Because of this, I made a custom binder which should handle byte, decimal, etc numbers fine. See below:

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;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:19:17+00:00Added an answer on June 7, 2026 at 9:19 pm

    You could send them as strings:

    { 
        "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 you are interested in more details you may checkout the following post.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am posting a json object to an ASP.Net MVC controller with C# code.
I am using ASP MVC with JQuery and posting json data to my controller.
I am posting Json data to a Restful Api. The server ASP.Net MVC app
I am posting JSON data to an ASP.NET MVC2 server. I am posting large
In my ASP.NET MVC 2 web application, I allow users to create custom input
I am POSTing data to an MVC controller and I'm attempting to maintain state
I am having problems with an Ajax/jQuery postback in ASP.NET MVC 3. If validation
I'm having some problems posting JSON data to my rails controller and getting the
I have an ASP.NET MVC Form, but the data from it needs to be
My asp.Net MVC app needs to store a byte array that represents a user-selected

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.