asp.net mvc - How to tell the default model binder to ignore a property on postback -
so i've fee abstract class in models (generated ef database first in mind)
public abstract partial class fee { public int feeid { get; set; } public int productid { get; set; } public string name { get; set; } public decimal standardprice { get; set; } }
it's inherited number of others classes model different kinds of fees, e.g. quarterlyfee kind of stuff. have customer class in model , customers can have custom rates, modeled class customerrate.
now on customer detail page i'm trying display possible fees customer user can pick rates custom , standards, no bid deal create viewmodel class possiblefee model :
public class possiblefee { public int customerid { get; set; } public int feeid { get; set; } public boolean customrate { get; set; } public decimal customerprice { get; set; } public virtual customer customer { get; set; } public virtual fee fee { get; set; } }
and added property customer class hold possible fees (icollection possiblefees), populate property in controller method (public actionresult details(int id = 0)), editortemplate later, , bam i've list of possible fees customer depending on products use user can edit.
now on postback, when hit save button, default model binder job , try populate icollection of possiblefee, , each possiblefee try create fee object populate property of same name.
of course doesn't work since fee abstract, question rather simple:how tell defaultmodelbinder ignore property on postback, because don't need on postback ?
i know recopy fields need on editortenplate of possiblefee in class possiblefee, or remove abstract fee. want know if can tune defaultmodelbinder needs without touching model.
edit 2 : yarx offered cleaner solution in opinion, see 2 posts bellow
edit : carlos corral carvajal got right, method setproperty called after modelbinder tries create object, had overwrite bindproperty instead
public class custommodelbinder : defaultmodelbinder { protected override void bindproperty(controllercontext controllercontext, modelbindingcontext bindingcontext, propertydescriptor propertydescriptor) { if (propertydescriptor.attributes.contains(new modelbinderignoreonpostback())) { return; } base.bindproperty(controllercontext, bindingcontext,propertydescriptor); } } [attributeusage(attributetargets.property, allowmultiple=false, inherited=false)] public class modelbinderignoreonpostback : attribute {}
added custom attribute in case needs
my suggestion separate viewmodels have separate viewmodel per action. have viewmodel that's displaying information , 1 used accepting information post back. because in cases majority of fields want bind on postback needed in viewmodel used display data in first place easiest thing inherit viewmodels. see below example.
public class possiblefeeinputmodel { public int customerid { get; set; } public int feeid { get; set; } public boolean customrate { get; set; } public decimal customerprice { get; set; } public virtual customer customer { get; set; } }
.
public class possiblefeeviewmodel : possiblefeeinputmodel { public virtual fee fee { get; set; } }
this way use possiblefeeviewmodel display view, use possiblefeeinputmodel bind against when accept postback.
Comments
Post a Comment