Re: MVC - Model binding to collection



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@xxxxxxxxxxxxxxxxxxxxxxx
I 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@xxxxxxxxxxxxxxxx
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/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



.



Relevant Pages

  • Re: Decoding strategy
    ... Mapping is actually two-step process, ... address at which to map the file, this isn't necessary, nor does it to my ... needs physical RAM that's not available). ... This doesn't reserve any ...
    (microsoft.public.dotnet.languages.csharp)
  • behavior as mapping
    ... seeing behavior as a mapping from a very high dimension ... that the map has been changed. ... The brain has a finite and relatively fixed number of neurons, ... But in fact, before we learned how to ride a bike, ...
    (comp.ai.philosophy)
  • Re: Binary Tree and Pairs of Nodes
    ... Every node in your infinite binary tree occurs at the ... So you say you have a mapping of every path to a node. ... You do not map My path to 1, because whichEVER path YOU mapped to 1, ...
    (sci.logic)
  • Re: behavior as mapping
    ... seeing behavior as a mapping from a very high dimension ... that the map has been changed. ... The brain has a finite and relatively fixed number of neurons, ... But in fact, before we learned how to ride a bike, ...
    (comp.ai.philosophy)
  • Re: Decoding strategy
    ... Mapping is actually two-step process, ... address at which to map the file, this isn't necessary, nor does it to my ... needs physical RAM that's not available). ... There are reasons that you might not be able to map an entire file into your ...
    (microsoft.public.dotnet.languages.csharp)

Loading