R
RichB
I am slowly getting to grips with the default model binding within the MVC
framework, but within the model I am looking at now the model will not bind
for a collection.
I have read Scott Hanselman's blog post:
http://www.hanselman.com/blog/ASPNE...dingToArraysListsCollectionsDictionaries.aspx
I have then managed to replicate it for relatively simple examples where a
Model object contains a collection of simple objects. This when passed to the
view is represented in the view with HTML input field as follows:
<%= Html.TextBox("object.Collection[index].PropertyName")%>
This maps back to recreate the object including the collection Properties
when the form is posted back.
I am however having difficulty in getting the mapping to work in a more
complex example:
<% foreach (var contact in Model.Venue.VenueDetail.ContactLink.ContactDatas)
{%>
<p>
<label
for="venue.VenueDetail.ContactLink.ContactDatas[<%=Model.Venue.VenueDetail.ContactLink.ContactDatas.IndexOf(contact)%>].Data">Indoor or Outdoor:</label>
<%=
Html.TextArea("contact["+Model.Venue.VenueDetail.ContactLink.ContactDatas.IndexOf(contact)+"].Data")%>
<%=
Html.ValidationMessage("venue.VenueDetail.ContactLink.ContactDatas["+Model.Venue.VenueDetail.ContactLink.ContactDatas.IndexOf(contact)+"].Data", "*")%>
</p>
<%} %>
This appears to create the correct html, i.e. the index starts at 0, and
follows sequentially. (I have checked many times that this is the correct
path to the object/property (intellisense provides the following path:
Model.Venue.VenueDetail.ContactLink.ContactDatas[0].Data).
When submitting the form it passes back the following in the post:
[reduced for
brevity].....venue.VenueDetail.ContactLink.ContactDatas%5B0%5D.Data=fred&venue.VenueDetail.ContactLink.ContactDatas%5B1%5D.Data=wilma
However, whilst the ContactDatas object is created, it is not populated with
any ContactData objects.
Should the default model mapper map to models 3 deep, and if so is there any
obvious error in what I have stated above.
If not, then how should I best provide mappings for my ContactData objects?
Thanks,
Richard
framework, but within the model I am looking at now the model will not bind
for a collection.
I have read Scott Hanselman's blog post:
http://www.hanselman.com/blog/ASPNE...dingToArraysListsCollectionsDictionaries.aspx
I have then managed to replicate it for relatively simple examples where a
Model object contains a collection of simple objects. This when passed to the
view is represented in the view with HTML input field as follows:
<%= Html.TextBox("object.Collection[index].PropertyName")%>
This maps back to recreate the object including the collection Properties
when the form is posted back.
I am however having difficulty in getting the mapping to work in a more
complex example:
<% foreach (var contact in Model.Venue.VenueDetail.ContactLink.ContactDatas)
{%>
<p>
<label
for="venue.VenueDetail.ContactLink.ContactDatas[<%=Model.Venue.VenueDetail.ContactLink.ContactDatas.IndexOf(contact)%>].Data">Indoor or Outdoor:</label>
<%=
Html.TextArea("contact["+Model.Venue.VenueDetail.ContactLink.ContactDatas.IndexOf(contact)+"].Data")%>
<%=
Html.ValidationMessage("venue.VenueDetail.ContactLink.ContactDatas["+Model.Venue.VenueDetail.ContactLink.ContactDatas.IndexOf(contact)+"].Data", "*")%>
</p>
<%} %>
This appears to create the correct html, i.e. the index starts at 0, and
follows sequentially. (I have checked many times that this is the correct
path to the object/property (intellisense provides the following path:
Model.Venue.VenueDetail.ContactLink.ContactDatas[0].Data).
When submitting the form it passes back the following in the post:
[reduced for
brevity].....venue.VenueDetail.ContactLink.ContactDatas%5B0%5D.Data=fred&venue.VenueDetail.ContactLink.ContactDatas%5B1%5D.Data=wilma
However, whilst the ContactDatas object is created, it is not populated with
any ContactData objects.
Should the default model mapper map to models 3 deep, and if so is there any
obvious error in what I have stated above.
If not, then how should I best provide mappings for my ContactData objects?
Thanks,
Richard