Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default value using data annotation

I am learning ASP.Net MVC 5 and I want to set default value using data annotation for boolean property. Also I don't want to use the constructor to set the default value. Is it possible?

public class BalanceDetailMV
{
    public BalanceDetailMV()
    {
        this.isUnitNoEmptyInAllRow = true; // I do not want this
    }
    public bool isUnitNoEmptyInAllRow { get; set; }
}

My attmept:

[DefaultValue("true")]
 public bool isUnitNoEmptyInAllRow { get; set; }

But above does not work. Please guide me.

like image 410
Unbreakable Avatar asked Jun 16 '17 14:06

Unbreakable


Video Answer


1 Answers

You might be having error if forgot to add using System.ComponentModel; at the top of your file where you use DefaultValue annotation.

For bool use

[DefaultValue(true)] 
public bool IsUnitNoEmptyInAllRow { get; set; }
like image 167
Developer Marius Žilėnas Avatar answered Oct 04 '22 04:10

Developer Marius Žilėnas