Thoughts from Dan Miser RSS 2.0
# Tuesday, February 23, 2010
I use list binding in my ASP.NET MVC applications in several places. When it works, it is truly magnificent. When it doesn't, it's just maddening. Here's a quick heads up for those of you that use list binding in MVC. You would expect the following 2 lines to render the same HTML, but they don't. To get list binding to work, you need the HTML to read something like this (I'm just listing the attributes in question here):


<input name="Results[0].Score" />

The old TextBox helper works fine, since you're assigning the name attribute:


Html.TextBox("Results["+id+"].Score", Model.Score)

This code, however:


Html.TextBoxFor(r => r.Score, new { name = "Results[" + id + "].Score" })

produces the following output, which means the Score property won't get bound properly.


<input name="Score" />

The problem is that in the TextBoxFor code, the name attribute is ignored. To be more precise, the custom name is added, but the attribute is then replaced by the name derived from the model.

Tuesday, February 23, 2010 10:12:13 PM (Central Standard Time, UTC-06:00)  #    Comments [3] -
.NET | ALT.NET | ASP.NET MVC
Navigation
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Dan Miser
Sign In
Statistics
Total Posts: 375
This Year: 3
This Month: 0
This Week: 0
Comments: 654
Themes
Pick a theme:
All Content © 2012, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)