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 9244089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:56:41+00:00 2026-06-18T08:56:41+00:00

I am receiving the following VM on a Web API Post action public class

  • 0

I am receiving the following VM on a Web API Post action

public class ViewModel
{
    public string Name { get; set; }

    [Required]
    public int? Street { get; set; }
}

When I make a post I get the following error:

Property ‘Street’ on type ‘ViewModel’ is invalid. Value-typed properties marked as [Required] must also be marked with [DataMember(IsRequired=true)] to be recognized as required. Consider attributing the declaring type with [DataContract] and the property with [DataMember(IsRequired=true)].

It seems the error is clear so I just want to be completely sure that it is required to use [DataContract] and [DataMember] attributes when you have a class with required nullable properties.

Is there a way to avoid using these attributes in Web API?

  • 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-18T08:56:42+00:00Added an answer on June 18, 2026 at 8:56 am

    I’m facing the same problem as you, and I think it’s complete nonsense. With value types I can see that [Required] doesn’t work since a value-typed property can’t be null, but when you’ve got a nullable value type there shouldn’t be any issue. However, the Web API model validation logic seems to treat non-nullable and nullable value types the same way, so you have to work around it. I found a work-around in the Web API forum and can confirm that it works: Create a ValidationAttribute subclass and apply it instead of RequiredAttribute on nullable value-typed properties:

    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Web.Mvc;
    
    public class NullableRequiredAttribute : ValidationAttribute, IClientValidatable
    {
        public bool AllowEmptyStrings { get; set; }
    
        public NullableRequiredAttribute()
            : base("The {0} field is required.")
        {
            AllowEmptyStrings = false;
        }
    
        public override bool IsValid(object value)
        {
            if (value == null)
                return false;
    
            if (value is string && !this.AllowEmptyStrings)
            {
                return !string.IsNullOrWhiteSpace(value as string);
            }
    
            return true;
        }
    
        public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
        {
            var modelClientValidationRule = new ModelClientValidationRequiredRule(FormatErrorMessage(metadata.DisplayName));
            yield return modelClientValidationRule;
        }
    }
    

    NullableRequiredAttribute in use:

    public class Model
    {
        [NullableRequired]
        public int? Id { get; set; }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using adLDAP.php class receiving following error: Notice: Undefined index: memberof in /web/ee_web/include/adLDAP.php on line
I'm working with a web API, which is returning the following data (this is
I created a web service client and I am receiving the following error when
I am consistently receiving the following error when trying to create a Web Reference
I'm using Ninject 2 and the Ninject.Web.MVC and using the NinjectHttpApplication Receiving the following
I am receiving the following error in my web.config. Parser Error Message: Object reference
I have a file with the following structure: system.action.webMessage=An error has happened during web
I am receiving the following error message when trying to display my web part.
I am receiving the following error message after an HTTP POST on an ASP.NET
I have a Jersey web service with the following a resource class: @Stateless @Path(/provision)

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.