4

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

2 Answers 2

9

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; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.