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

  • Home
  • SEARCH
  • 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 8376619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:28:18+00:00 2026-06-09T15:28:18+00:00

Can’t pass ModelState validation in WebApi application for object which contains nullable types and

  • 0

Can’t pass ModelState validation in WebApi application for object which contains nullable types and has null values. The error message is “The value ‘null’ is not valid for DateProperty.”

The code of object:

public class TestNull
{
    public int IntProperty { get; set; }
    public DateTime? DateProperty { get; set; }
}

Controller:

public class TestNullController : ApiController
{
    public TestNull Get(int id)
    {
        return new TestNull() { IntProperty = 1, DateProperty = null };
    }

    public HttpResponseMessage Put(int id, TestNull value)
    {
        if(ModelState.IsValid)
            return Request.CreateResponse(HttpStatusCode.OK, value);
        else
        {
            var errors = new Dictionary<string, IEnumerable<string>>();
            foreach (var keyValue in ModelState)
            {
                errors[keyValue.Key] = keyValue.Value.Errors.Select(e => e.ErrorMessage);
            }

            return Request.CreateResponse(HttpStatusCode.BadRequest, errors);
        }
    }
}

Request:

$.getJSON("api/TestNull/1",
function (data) {
    console.log(data);
    $.ajax({
        url: "api/TestNull/" + data.IntProperty,
        type: 'PUT',
        datatype: 'json',
        data: data
    });
});
  • 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-09T15:28:19+00:00Added an answer on June 9, 2026 at 3:28 pm

    I just did a quick test in one my own WebAPI projects and passing null as a value for a nullable value-type works fine. I suggest you inspect the actual data that is being send to your server using a tool like Fiddler

    Two valid scenarios that will work are:

    { IntProperty: 1, DateProperty: null } 
    { IntProperty: 1 } // Yes, you can simply leave the property out
    

    Scenarios that will NOT work are:

    { IntProperty: 1, DateProperty: "null" } // Notice the quotes
    { IntProperty: 1, DateProperty: undefined } // invalid JSON
    { IntProperty: 1, DateProperty: 0 } // Will not be properly interpreted by the .NET JSON Deserializer 
    

    If the two default scenario’s do not work then I suspect your problem lies elsewhere. I.e. Have you changed any of the default settings of the JSON serializer in your global.asax?

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

Sidebar

Related Questions

Can any one tell, how to get the result of LINQ query contains group
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object
Can someone help me with this scenario? *There is a button, which when tapped,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
can anyone tell me if it is possible to convert a dojo charting object
can anyone tell me why this doesn't work? db = openOrCreateDatabase(database.db, SQLiteDatabase.CREATE_IF_NECESSARY, null); db.setLocale(Locale.getDefault());
Can a LINQ enabled app run on a machine that only has the .NET
Can I somehow see the types and size of the contents of a tuple?
Can anyone let me know how can we change the value of kendo combobox
Can I order my users in the database, so I don't have to say

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.