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
Post a Comment