How to get dynamic paramter in asp.net mvc post method -
i'm trying make of post methods this:
client side:
$.post("/control/postmethod",{a:1,b:2,c:[1,2,3],d:{x:1,y:0}})
server side:
[httppost] public int postmethod(dynamic model) { //do model.a model.b etc. return 1; }
the problem if did nothing,the model seems 1 simple object no property,so tried write custommodelbinder
replace defaultmodelbinder
, override createmodel
method analyse form values.but found became difficult because properties had been expanded,like property d
became a[d][x],a[d][y]
in form values.
so there simple way transfer client post data dynamic object in actions?
if able supply information type use custom model binder , supply necessary type infos. have modelbinder scans "type" querystring-value used feed modelbinder information needs.
public class mymodelbinder : defaultmodelbinder { public override object bindmodel( controllercontext controllercontext, modelbindingcontext bindingcontext ) { // check if model type object if (bindingcontext.modeltype == typeof( object )) { // try type info query string or form-data var type = controllercontext.httpcontext.request.querystring["type"] ?? controllercontext.httpcontext.request.form["type"]; if (type != null ) { // find way type info, example var matchingtype = assembly.getexecutingassembly().gettype(type); // supply metadata our bindingcontext , default binder rest bindingcontext.modelmetadata = modelmetadataproviders.current.getmetadatafortype( null, matchingtype ); } } return base.bindmodel( controllercontext, bindingcontext ); } }
my controller method looks this:
public actionresult method( string type, object model ) { }
Comments
Post a Comment