tags:

views:

61

answers:

3

I've got a view which is adding a bunch of data into a bound EditModel. However, when it is POSTed, for some reason a parameterless constructor is called before calling the POST Action, instead of just passing back the model that was bound to the view.

I thought all I had to do was to make sure I reference the model in the "Inherits" tag at the top of the view, but for some reason all of the filled in data just gets thrown away on POST and a new instance of the model is instantiated. The View certainly seems to be able to see the model, as I can use Visual Studio Intellisense to access the model fields.

I'm sure it is something simple I am missing, so any pointers as to where to look would help. I can paste in some code if it helps.

Thanks!

A: 

The constructor it is called, cuz that's what it should happen, now to see why is your model not filled with data it would be very helpful if would post the code of the view and post action

Omu
A: 

This is not how MVC works. When form is posted, MVC constructs new EditModel object and populates it with values provided in form. It doesn't preserve EditModel with was used to populate view in GET action. You have to make sure that every value that you want to use in POST method is set in html inputs in GET.

LukLed
+2  A: 

As Omu says it will always call the parameterless constructor with a default modelbinder. Basically the modelbinder populate public memberes from the form collection based on the element names (plus maybe a prefix if defined). You need to make sure your element names (not Ids) match the public members you want populating on the model.

Pharabus
Right, I think I've got my head screwed on correctly now, and see that the calling of the constructor a second time is not my problem (thanks for the background info on that). I double checked all of the element names and I seem to have that sorted out now, sothat was my problem (I had adjusted my model data and missed some corresponding element name changes in the view). Thanks very much to all for the help. Still having trouble getting values back from dropdowns, but I will post separately on that and include some code.Chris
Chris