In my continuing effort to become educated on the MVC framework, I stubbed my toe on something today.
I setup a simple edit screen and I wanted to updated the data on that screen to the database. I built my edit form by using the Html helper methods (Html.TextBox to be exact), but I made a few mistakes that cost me a few minutes of headaches and I thought I would share.
Issue 1
During my coding I created the first text box and then copy/pasted to create my next text box. However, I failed to change the name of the text box and this caused me my first pain. When I ran the controller and called the UpdateFrom method I got weird values populated back into my object, see below.
Notice how the Name property has 2 values separated by a ',' this is because my Html was as follows.
| <tr> <td width='150'>Name:</td> <td width='200'><%=Html.TextBox( "Name", ViewData.Name, 50 ) %></td> </tr> <tr> <td width='150'>Description:</td> <td width='200'><%=Html.TextBox("Name", ViewData.Description, 50)%></td> </tr> If you notice, the text box name for BOTH text boxes are "Name", this is what caused the issue. | |
Issue 2
The next issue I came across was when my text box values where NOT be populated back into the object correct. This happened when the name of my text box (would be the same for any control) did not map to a property on the object.
The image above shows the values have not changed (yes, they are meant to be the same in this case) because of my Html was a follows.
<tr>
<td width='150'>Name:</td>
<td width='200'><%=Html.TextBox( "Name", ViewData.Name, 50 ) %></td>
</tr>
<tr>
<td width='150'>Description:</td>
<td width='200'><%=Html.TextBox("DescriptionWRONGNAME", ViewData.Description, 50)%></td>
</tr>
If you notice, the name of my text box did NOT match that of my property, this was my issue.
It would be nice if the various Html helpers would have an override where the name of the control was populated as the name of the property populating the control.
Till next time,