haacked / feedback

Ask @haacked anything!
7 stars 1 forks source link

Unexpected Model Binding Behaviour #154

Closed andypolshaw closed 7 years ago

andypolshaw commented 7 years ago

Hi there. I'm contacting you after searching the Web, not finding my "bug" mentioned anywhere and getting advice that you're the man to contact. I used your Professional MVC 4 Wrox book to initially get a fuller understanding of MVC before I got the MS certification in it.

I've been writing an intranet app and I've been using jQuery ajax (rather than the built in generated MS JS handlers) to send and receive data from my site. I've also been making heavy use of partial views. I sent a JSON object via AJAX to populate the parameters, and that object contains a property called "name". I then retrieved a new ViewModel via my facade classes and that had a property called Name and one of its properties contained an array of other ViewModels, each of which also had a property called Name.

Now, the other property values were bound correctly (despite having repeated field names - each "row" is wrapped in its own form tag) but if the main or any of its nested viewmodels contain a property called Name, despite it being bound correctly before being sent back to the client, it replaces the textbox values with the value that was submitted via Ajax to the website.

I cannot find any mention that this would happen anywhere on the Web or in your or other books I've read FYI, this is (obviously) an edit mode view. It's as if something preserves what was sent and writes over the form input values that are sent back. Why does it do this? What i am I missing? For now, I just renamed my ViewModel properties to StepName and PropertyName and it fixed it.

Thanks.

haacked commented 7 years ago

IIRC, MVC is case insensitive when it comes to binding properties. So the name and Name fields may be stomping on each other.