c# - Default value for Create form in mvc -
i set default value of text area in asp.net mvc 5 application. have field in patient model looks this:
[display(name = "date of birth")] [datatype(datatype.date)] [displayformat(dataformatstring = "{0:yyyy-mm-dd}", applyformatineditmode = true)] [required] public datetime dateofbirth { get; set; }
and set default value of field current date. right displaying form : http://scr.hu/11m6/lf9d5 . have tried using constructor , setting value of dateofbirth in :
public patient() { dateofbirth = datetime.now; }
but had no effect. tried editing view .cshtml file this:
@html.editorfor(model => model.dateofbirth, new { @value = "2014-05-05" })
but had no effect. know solution ?
you should create instance of patient
class , pass view in create
action. in case view model not set, not enters patient
class constructor , not use datetime.now
value displayed.
try changing create
action method from:
// get: /patient/create public actionresult create() { return view(); }
to:
// get: /patient/create public actionresult create() { var patient = new patient(); return view(patient); }
Comments
Post a Comment