How to set a fixed value attribute in ASP.Net MVC?

4

I have a user class that has the attribute permissão , which should always be 1. Where and how would I do to set this value? In the folder models , controller or in the view of cadastre?

    
asked by anonymous 04.09.2014 / 21:05

2 answers

4

No Model, more specifically in the constructor:

public class Usuario
{
    ...
    public int Permissao { get; set; }

    public Usuario() {
        Permissao = 1;
    }
}

To prevent modifying the value (read-only property), you can do the following:

public class Usuario
{
    ...
    public int Permissao { get { return 1; } }
}

To prevent the value from being mapped in the Entity Framework (if you are using it), use [NotMapped] :

public class Usuario
{
    ...
    [NotMapped]
    public int Permissao { get { return 1; } }
}
    
04.09.2014 / 21:34
0

I would use:

public class Usuario
{
...
 public const int Permissao = 1;
}

If it is always just a number without rules, no validations with nothing, then it makes perfect sense to be a constant and ready:)

    
27.08.2015 / 19:35