asp.net mvc 3 - Annotating properties on a model with default values -


i created ef4.1 code-first model (may or may not important), , i'm trying default values create scaffold template. model looks like:

class person {     [defaultvalue (18)]     public int age { get; set; } } 

and create view looks like:

<div class="editor-label">     @html.labelfor(model => model.age) </div> <div class="editor-field">     @html.editorfor(model => model.age)     @html.validationmessagefor(model => model.age) </div> 

i expect @ runtime, editorfor pre-populate textbox "18", no such thing. misunderstanding defaultvalue attribute for, or there else should doing?

note: don't want use new { value = "18" } override on editorfor method, seems break dry.

i don't know if satisfy dry needs, it's start think.

i rework model bit this:

public class person {     private const int default_age = 18;     private int _age = default_age;     [defaultvalue(default_age)]     public int age {         { return _age; }         set { _age = value; }     } } 

keep view is, in create action this:

public actionresult create() {     return view(new person()); } 

that way, input textbox created default age value, , there 1 place default specified.


Comments

Popular posts from this blog

linux - Using a Cron Job to check if my mod_wsgi / apache server is running and restart -

actionscript 3 - TweenLite does not work with object -

jQuery Ajax Render Fragments OR Whole Page -