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.
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.