Re: MVC - Model binding to collection
- From: "Cowboy \(Gregory A. Beamer\)" <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx>
- Date: Thu, 21 May 2009 09:21:16 -0500
Are you using ViewPage or ViewPage<T>?
If you use ViewPage<T>, you have a more explicit way of defining your model (ie, T) so you can drill through more explicitly.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Blog: http://gregorybeamer.spaces.live.com
Twitter: @gbworld
*************************************************
| Think outside the box! |
*************************************************
"RichB" <richsanswers@xxxxxxxxxxxxxxxx> wrote in message news:#iCVv1#1JHA.3304@xxxxxxxxxxxxxxxxxxxxxxx
Sorry,.
perhaps I should be clearer, I am wanting to create an object which has a collection. So a Venue has some details part of which are a series of contact details (phone, email etc). I am creating the view without problem, but my expectation is that I can create a textbox with a name which will enable the defaultmodelbinder to map back the post variables to the model.
I'm sure that my model could be a little simpler, although I would still have Contact Data as a collection and it is complicated a little because of the linqtosql mapping. I guess that my question boils down to whether the defaultModelBindershould be able to map to a model of this complexity or whether I need to do some work to simplify it or if there is a more appropriate way to perform the mapping.
Thanks,
Richard
"Cowboy (Gregory A. Beamer)" <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx> wrote in message news:OJJd$Y91JHA.4880@xxxxxxxxxxxxxxxxxxxxxxxI got a headache looking at what you posted, but if I understand the gist of it, you are wondering how to get an individual item from a list? If so, you have to simplify. You should not have to pass a godawfully long string of HTTP encoded crap to get info on a single item. If you want to drill down n number of steps in a hierarchy, create a page to display that item so you can get it with a simple:
http://mysite/item3/1
type of URL. You will have to deconstruct your hierarchy a bit to get here, but linking back with a huge query string will likely end up with a very kludgy and buggy application.
Hope this helps.
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
*************************************************
| Think outside the box! |
*************************************************
"RichB" <richsanswers@xxxxxxxxxxxxxxxx> wrote in message news:773AB148-9814-4F45-96D0-5997283E38E4@xxxxxxxxxxxxxxxxI 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/ASPNETWireFormatForModelBindingToArraysListsCollectionsDictionaries.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
- Follow-Ups:
- Re: MVC - Model binding to collection
- From: funka420
- Re: MVC - Model binding to collection
- References:
- MVC - Model binding to collection
- From: RichB
- Re: MVC - Model binding to collection
- From: Cowboy \(Gregory A. Beamer\)
- Re: MVC - Model binding to collection
- From: RichB
- MVC - Model binding to collection
- Prev by Date: Re: Visual Studio 2008 Professional Edition Web Form Controls
- Next by Date: Re: Problem cleaning up temp folder after saving files to database
- Previous by thread: Re: MVC - Model binding to collection
- Next by thread: Re: MVC - Model binding to collection
- Index(es):
Relevant Pages
|
Loading