开发者

asp.net mvc data annotation for default value

开发者 https://www.devze.com 2023-02-04 21:55 出处:网络
Is there a default value attribute that can be used in ASP.NET MVC to set a default attribute for an input i.e. for Create forms.

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

There is such an attribute in System.ComponentModel but it does not have any effect.

开发者_开发知识库Thanks Ben


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

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


If you really want Attribute solution then this isn't what you probably want, but I use ViewModel's constructor to set default value for the controls.

public class BookViewModel
{
    public int Amount { get; set; }

    public BookViewModel()
    {
        Amount = Constants.default_amount_for_book_order;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号