update binding hidden fields of viewmodel.
let me try explain situation. may wrong believe causing issue me.
i have viewmodel
project create view model
[bind(exclude="list")] public class projectcreateviewmodel : projectviewmodelbase { public courseviewmodelbase coursevm { get; set; } public projectcreateviewmodel() : base() { this.coursevm = new courseviewmodelbase(); } }
project view model base base viewmodel project , associated actions derive don't need write property names again , again.
create view model base similar projectviewmodelbase(handled or used projectcontroller) course (handled coursecontroller).
now i've created form "create new project" uses projectcreateviewmodel. in form post action coursevm null.
create new project .cshtml
@model lmspriortool.viewmodels.projectcreateviewmodel @* --- labels , other stuff -- *@ @using (html.beginform("createnewproject", "project", formmethod.post, new { @class = "form-horizontal", name = "createnewprojectform" })) { <!-- hidden fields --> @html.hiddenfor( x => x.projectid) @html.hiddenfor( x => x.coursevm) // coursevm null in post action @html.textboxfor(x => x.coursevm.coursenumberroot) // displayed }
projectcontroller
[httpget] public actionresult createnewproject(courseviewmodelbase coursevm = null) { projectcreateviewmodel projectcreateviewmodel = new projectcreateviewmodel { coursevm = coursevm, }; // other stuff return view("createnewproject", projectcreateviewmodel); }
error in httppost action i'm getting coursevm null, though have provided hidden field in form.
possible issue belive issue constructor of projectcreateviewmodel when httppost action occur, view try create new instance of projectcreateviewmodel , instantiate coursevm null. same instance passed httppost method in coursevm appearing null.
update: issue root cause complex objects cannot bind viewmodel using hidden fields.
any suggestions or thoughts appreciated.
you don't need hiddenfor
of coursevm
. mvc create class automatically because binding coursevm.coursenumberroot
at moment, attempting bind coursevm
, complex object, hidden input mvc can't do, returning null.
Comments
Post a Comment