Asp.net - MVC: Model binding to Collection Editing

  • Thread starter Thread starter RichB
  • Start date Start date
R

RichB

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.
 
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 (e-mail address removed). 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:
(e-mail address removed).

==================================================
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,

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
 
Back
Top