I have a Model that I can bind correctly for display purposes but I can't post an updated version.
I can display a page and its list of contents in a form for updating in a strongly typed view.
But when I post an updated model i.e. if I changed a pagecontents content then the posted model contains the Pages details but loses the relationship between the page and the pagecontent.
So when I get the Page for display purposes the expanded PageContents Page contains the correct deatils.
But when I update and Post PageContents contains no Page entry.
(BTW may be a red herring but I am using Fluent nHibernate)
Unfortunately I can't find what I am missing.
So here's an abridged version of my code:
public class Page
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual string Title { get; set; }
public virtual IList<PageContent> PageContents { get; private set; }
public Page()
{
PageContents = new List<PageContent>();
}
public virtual void AddPageContent(PageContent pageContent)
{
pageContent.Page = this;
PageContents.Add(pageContent);
}
}
public class PageContent
{
public virtual int Id { get; private set; }
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual string Content { get; set; }
public virtual Page Page { get; set; }
}
<% using (Html.BeginForm()) {%>
Page <%= Html.LabelFor(model => model.Title) %>:
<%= Html.TextBoxFor(model => model.Title, new { @class = "wide-input" })%>
<%= Html.ValidationMessageFor(model => model.Title) %>
Page <%= Html.LabelFor(model => model.Name) %>:
<%= Html.TextBoxFor(model => model.Name, new { @class = "wide-input" })%>
<%= Html.ValidationMessageFor(model => model.Name) %>
<%
foreach (var item in Model.PageContents)
{
%>
<%= Html.TextAreaFor(m => item.Content) %>
<%
}
%>
<p><input type="submit" value="Save" /></p>
<% } %>