Custom Attribute null reference exception

落爺英雄遲暮 提交于 2019-12-25 18:21:19

问题


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.

来源:https://stackoverflow.com/questions/45392278/custom-attribute-null-reference-exception

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