Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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,


Posted 03-09-2008 2:04 PM by Derik Whittaker
Filed under: ,

[Advertisement]

Comments

Christopher Steen wrote Link Listing - March 9, 2008
on 03-10-2008 2:12 AM

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

Christopher Steen wrote Link Listing - March 9, 2008
on 03-10-2008 2:12 AM

Link Listing - March 9, 2008

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)