MVC Action Not Receiving Posted Data

I’ve had an issue which has consumed the majority of today: my MVC action, “Save”, with 1 parameter has been receiving an empty object as its parameter despite the form posting what looked like valid data. It seemed like the model binder was refusing to bind the posted data for some reason.

The situation was thus:

  • Form posting multiple properties of the ObjectBatch object in the format Id, Objects[0].Id, etc.
  • A Save action accepting a single parameter, objects of type ObjectBatch

Well, after a day of fiddling (and nearly jumping out the window a few times), I eventually discovered that the object was correctly creating the ObjectBatch until adding the Objects[x].y parameters.

Finally, my colleague @Benjimawoo suggested adding a second parameter for the sub-collection and adding them to the main object within the action.

Then it dawned on me… MVC matches the parameter names to the name format of the posted fields. This meant that, after populating the ObjectBatch, it was recreating it using the Objects[x].y properties.

Simply renaming the parameter from ObjectBatch objects to ObjectBatch batch solved this!

I see exactly why its done like that… still a bit of a pain though!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s