c# - Custom binder works fine, but the model is not updated -
i have model , looks this:
public class mymodel { public list<somebaseclass> list {get; set;} public string someproperty {get; set;} }
where somebaseclass
in fact base of operations class , list can contain items of different types, types inherited somebaseclass
.
to create sure model binds properly, had implement custom binder fills out model based on form data.
public class mymodelbinder: defaultmodelbinder { public override object bindmodel(controllercontext cntxt, modelbindingcontext bindingcontext) { var model = new mymodel { list = new list<somebaseclass>(), someproperty = ... }; ... // info mangling , type twisting here homecoming model; // here debugger shows model's list populated based on form data. } }
but when view calls action, model not complete:
public string someaction(mymodel model) { // <~~ calls custom binder before coming here, right // result, model variable instance of mymodel, list null homecoming "somethhing"; }
in action method, receive model object list
property set null. strange, because binder called correctly , populates model , properties properly.
i can't figure out doing wrong.
p.s. when seek phone call updatemodel<mymodel>(model);
in action method, throws "the model of type mymodel not updated."
ok, got it. had add together binder attribute next model class declaration create sure binder called when model passed action method.
[modelbinder(typeof(mymodelbinder))] public class mymodel { ... }
it not obvious, because when attribute not present, custom binder still called, reason output not beingness sent action, default binder's output used instead.
c# model-view-controller model-binding
No comments:
Post a Comment