Asp.net - MVC: Model binding to Collection Editing

Discussion in 'ASP .Net' started by RichB, Jun 3, 2009.

  1. RichB

    RichB Guest

    I posted a question here previously:
    http://www.microsoft.com/communitie...40bca7c0&lang=en&cr=US&sloc=en-us&m=1&p=1#top

    This question was answered and I have successfully moved on, however I
    cannot manage to bind to the model when editing it.

    I have made the change to the model as suggested (adding _ContactDatas = new
    EntitySet<ContactData>(); to the setter for public EntitySet<ContactData>
    ContactDatas) and my post method is as follows:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(string submitButton, Guid id,
    FormCollection form)
    {
    Venue venue = venueRepository.GetVenue(id);


    try
    {
    UpdateModel(venue, "Venue", form.ToValueProvider());
    switch (submitButton)
    {
    case "Save":
    {
    venueRepository.Save();
    return RedirectToAction("Details", new { id =
    venue.VenueID });
    }
    case "AddContact":
    {
    return AddContact(venue);
    }
    default:
    {
    throw new Exception("invalid form value");
    }
    }
    }
    catch
    {
    ModelState.AddRuleViolations(venue.GetRuleViolations());
    return View(new VenueFormViewModel(venue));
    }
    }


    My understanding is that UpdateModel should use the default Model Binder to
    iterate through the Form collection and append any changes to the Model,
    however if I make a change to the EntitySet<ContactData> ContactDatas
    information (either in the form of an update, or in the form of an add() no
    change to the ContactDatas object occurs (i.e. the brakpoint in the setter
    for the ContactDatas property is not reached.

    Any ideas??

    Thanks,
    Richard

    ----

    As an aside, I had a demo project for the previous question posted into
    which I hard coded an index. To test this I attempted to add the number of
    records dynamically with a for loop as follows:

    <%for (int i = 0; i < Model.VenueDetail.ContactLink.ContactDatas.Count; i++)
    {%>
    <p>
    <label
    for="venue.VenueDetail.ContactLink.ContactDatas.Data">Data:</label>

    <%=Html.TextBox("venue.VenueDetail.ContactLink.ContactDatas["+i.ToString()+"0].Data")%>
    </p>
    <%} %>

    Unfortunately I get an null reference exception in the for (...) line of
    code. If I put this line into my controller, with a
    trace.Writeline(i.ToString); (Replacing Model with venue - return
    View(venue)) the output shows that iterating through the loop is a success!!
    Therefore to provide a full example I need to understand this first.
     
    RichB, Jun 3, 2009
    #1
    1. Advertising

  2. Hi Richard,

    >however if I make a change to the EntitySet<ContactData> ContactDatas
    >information (either in the form of an update, or in the form of an add()

    no
    >change to the ContactDatas object occurs (i.e. the brakpoint in the setter
    >for the ContactDatas property is not reached.


    Could you clarify what do you mean by this? Where do you change the value
    of properties of ContactDatas? Could you provide detailed instruction as to
    how to reproduce it?

    I suggest you send a repro project to me first. I'll test it to see what's
    the problem and tell you the cause of anything you have interest with. My
    email is . Please update here after sending the project
    in case I missed that email.

    Regards,
    Allen Chen
    Microsoft Online Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Allen Chen [MSFT], Jun 4, 2009
    #2
    1. Advertising

  3. RichB

    RichB Guest

    Allen,

    I'm really sorry, I was getting increasingly frustrated yesterday and
    couldn't create the example as it kept falling over, however I had made a
    stupid mistake in the for loop: not passing the object to the view!!

    Anyway, I've got past this hurdle, and I have it working in my demo app, so
    I should be able to get it working in my proper app too.

    Sorry for wasting you time..

    Richard


    "Allen Chen [MSFT]" <> wrote in message
    news:p...
    > Hi Richard,
    >
    >>however if I make a change to the EntitySet<ContactData> ContactDatas
    >>information (either in the form of an update, or in the form of an add()

    > no
    >>change to the ContactDatas object occurs (i.e. the brakpoint in the setter
    >>for the ContactDatas property is not reached.

    >
    > Could you clarify what do you mean by this? Where do you change the value
    > of properties of ContactDatas? Could you provide detailed instruction as
    > to
    > how to reproduce it?
    >
    > I suggest you send a repro project to me first. I'll test it to see what's
    > the problem and tell you the cause of anything you have interest with. My
    > email is . Please update here after sending the
    > project
    > in case I missed that email.
    >
    > Regards,
    > Allen Chen
    > Microsoft Online Support
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 2 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions. Issues of this
    > nature are best handled working with a dedicated Microsoft Support
    > Engineer
    > by contacting Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    > ==================================================
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    >
    >
    >
    >
     
    RichB, Jun 4, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Øyvind Isaksen
    Replies:
    1
    Views:
    1,001
    Øyvind Isaksen
    May 18, 2007
  2. rmn190
    Replies:
    2
    Views:
    2,399
    Arne Vajhøj
    Jan 10, 2008
  3. RichB

    MVC - Model binding to collection

    RichB, May 18, 2009, in forum: ASP .Net
    Replies:
    14
    Views:
    3,027
    RichB
    May 28, 2009
  4. RichB
    Replies:
    9
    Views:
    1,136
    Allen Chen [MSFT]
    Jun 15, 2009
  5. Crazy Cat
    Replies:
    1
    Views:
    704
    schepp
    Sep 3, 2009
Loading...

Share This Page