asp.net mvc - Why are the nested objects in my parent object null when posting to my MVC 4 Controller? -


let's have complex model contains custom class:

    public class car {     public car() {}     public engine engine { get; set } // nested object     public int fuelremaining {get; set;}     public int vehiclestatus {get; set;} } 

and mvc 4 controller looks this:

    [httppost]     public partialviewresult showvehicledetails(car currentvehicle)     {         return partialview(currentvehicle);     } 

if pass instance of class controller using ajax post, , try return view, partial view, or peek object, of values of engine null, else fine.

here in example of json see showing in form data of request header post:

{"engine":{"enginevin":258736,"enginekm":160015,"enginedesc":"ecotech v8"},"fuelremaining":90,"vehiclestatus":1} 

note: values fuelremaining, , vehiclestatus showing fine in controller , in resulting view.

can explain me why case, , how best work around this?

i did come across this blog article asp.net team, dated 2010 , haven't been able find more recent information.

the objects model binding can hydrated values posted. hence if there no values in post corresponding asscoiated engine object none of properties populated.

the default model binding create new instance of object or objects sees in action method's parameter list. if model binding pull existing object, database, need custom model binder.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -