Asp.net mvc data annotations for default

Is there a default attribute that can be used in ASP.NET MVC to set a default attribute for input, i.e. to create forms.

There is such an attribute in System.ComponentModel, but it has no effect.

Thanks Ben

+4
source share
2 answers

You can set the default value in the constructor of your model:

public class MyViewModel { public MyViewModel() { SomeProp = "default value"; } public string SomeProp { get; set; } } 
+9
source

If you really need an Attribute solution, this is not what you probably want, but I use the ViewModel constructor to set the default value for the controls.

 public class BookViewModel { public int Amount { get; set; } public BookViewModel() { Amount = Constants.default_amount_for_book_order; } } 
+1
source

Source: https://habr.com/ru/post/1335547/


All Articles