Custom Attribute null reference exception

2019-09-14 02:41发布

问题:

I have an view model

public class fooViewModel
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    [CustomAttribute]
    public float foo1{ get; set; }
    [Required]
    [CustomAttribute]
    public decimal foo2 { get; set; }
}

I made a custom server side validation attribute.

[AttributeUsage(AttributeTargets.Property)]
public class CustomAttribute : RequiredAttribute
{
    public CustomAttribute()
    {
        ErrorMessage = "Some message";
    }

    public override bool IsValid(object value)
    {
        string input = value.ToString();
        //some validation logic
        return result;
    }
}

The problem is that my value parameter sometimes is null, but I can't figure out why.