Web API nullable required property requires DataMember attribute

痴心易碎 提交于 2019-12-31 09:12:12

问题


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:


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



回答2:


I think you are running into the same problem as discussed here:

DataAnnotation for Required property




回答3:


This is fixed in Web Api 2. However, interesting only works if the fields are properties with get/set.



来源:https://stackoverflow.com/questions/13633974/web-api-nullable-required-property-requires-datamember-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!