c# - ASP.NET: Modelbinder only delegating -
i'm facing problem ith custom modelbinder.
i have 2 models (inheriting base class) displayed editortemplates.
base-class:
public abstract class questionanswerinputmodel { public guid questionid { get; set; } }
modelclass 1:
public class ratingquestioninputmodel : questionanswerinputmodel{ [required] [range(1,4)] public int? rating { get; set; } }
modelclass 2:
public class freetextquestioninputmodel: questionanswerinputmodel{ [required] public string freetext { get; set; } }
to bound implemented custom modelbinder:
public class questionanswermodelbinder : defaultmodelbinder { public override object bindmodel(controllercontext controllercontext, modelbindingcontext bindingcontext) { questionanswerinputmodel model; if ((typeof(questionanswerinputmodel) != bindingcontext.modeltype)) { return null; } modelbindingcontext context = new modelbindingcontext(bindingcontext); type typeofmodel; string prefix = bindingcontext.modelname; if (bindingcontext.valueprovider.containsprefix(prefix + "." + new freetextquestioninputmodel().getpropertyname(m => m.freetext))) { typeofmodel = typeof(freetextquestioninputmodel); } else if (bindingcontext.valueprovider.containsprefix(prefix + "." + new ratingquestioninputmodel().getpropertyname(m => m.rating))) { typeofmodel = typeof(ratingquestioninputmodel); } else { return null; } context.modelmetadata = new modelmetadata(new dataannotationsmodelmetadataprovider(), bindingcontext.modelmetadata.containertype, null, typeofmodel, bindingcontext.modelname); return base.bindmodel(controllercontext, context); } }
all in works great, values fpr properties of models (questionid , rating/freetext) not set? can tell me why? doing wrong?
i tried call
new defaultmodelbinder().bindmodel(controllercontext, context)
but result same. correctly instantiated objects properties not set.
update:
i tried override createmodel-methode of defaultbinder in post mvc 3 model binding sub type (abstract class or interface).
protected override object createmodel(controllercontext controllercontext, modelbindingcontext bindingcontext, type modeltype) { if ((typeof(questionanswerinputmodel) != bindingcontext.modeltype)) { return null; } string prefix = bindingcontext.modelname; if (bindingcontext.valueprovider.containsprefix(prefix + "." + new freetextquestioninputmodel().getpropertyname(m => m.freetext))) { return new freetextquestioninputmodel(); } else if (bindingcontext.valueprovider.containsprefix(prefix + "." + new ratingquestioninputmodel().getpropertyname(m => m.rating))) { return new ratingquestioninputmodel(); } else { return null; } }
the model still instantiated correctly. problem is, properties of base class set.
after discussion @macpak found solution. works great me:
protected override object createmodel(controllercontext controllercontext, modelbindingcontext bindingcontext, type modeltype) { if ((typeof(questionanswerinputmodel) != bindingcontext.modeltype)) { return null; } string prefix = bindingcontext.modelname; questionanswerinputmodel obj; if (bindingcontext.valueprovider.containsprefix(prefix + "." + new freetextquestioninputmodel().getpropertyname(m => m.freetext))) { obj = new freetextquestioninputmodel(); } else if (bindingcontext.valueprovider.containsprefix(prefix + "." + new ratingquestioninputmodel().getpropertyname(m => m.rating))) { obj = new ratingquestioninputmodel(); } else { return null; } bindingcontext.modelmetadata = modelmetadataproviders.current.getmetadatafortype(null, obj.gettype()); bindingcontext.modelmetadata.model = obj; return obj; }
i have override createmodel-methode. @macpak!
Comments
Post a Comment