Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



MVC and BindingHelperExtension Gotcha's

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.

DoubledPropertyValue

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.

NoChangeInValue

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,



Comments

Christopher Steen said:

ASP.NET LinkButtons in UpdatePanel cause full postback unless you give them IDs [Via: alexcampbell ]...

# March 10, 2008 2:12 AM

Christopher Steen said:

Link Listing - March 9, 2008

# March 10, 2008 2:12 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Proudly Partnered With


This Blog

Syndication

News