How to replace all id attributes of a child collection of complex types using jQuery in ASP.net MVC
- by TJB
Here's my situation:
I'm writing an ASP.net MVC 1 website and I have a create/edit form that uses the default model binding to parse the form into a strongly typed complex object.
The object I'm posting has a child collection of another complex type and the way I format my id's for the model binder is as follows:
<div class="childContainer" >
<!-- There's one of these for each property for each child collection item -->
<%= Html.TextBox("ChildCollectionName[0].ChildPropertyName", /* blah blah */ ) %>
<%= Html.TextBox("ChildCollectionName[0].OtherChildPropertyName", /* blah blah */ ) %>
<!-- ... -->
</div>
This gets rendered as
<div class="childContainer" >
<input id="ChildCollectionName[0]_ChildPropertyName" ... />
<input id="ChildCollectionName[0]_OtherChildPropertyName" ... />
...
</div>
<div class="childContainer" >
<input id="ChildCollectionName[1]_ChildPropertyName" ... />
<input id="ChildCollectionName[1]_OtherChildPropertyName" ... />
...
</div>
For each entry in the chlid collection.
This collection is dynamically created in the form using jQuery, so entries can be added, removed etc. and whenever there's an operation on the collection I need to update the indexes so that it's bound correctly on the server side.
What's the best way to replace all the html input id's when I'm updating the index within the child e.g.
replace all [*] -- [N] where N is the correct index.
using jQuery / JavaScript ?
I have something coded now, but its buggy and I think there is a simpler solution.
Also, if you have an easier way to identify the child collection I'll take any advice on that as well.
Thanx!