Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1012901
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:00:57+00:00 2026-05-16T10:00:57+00:00

I have a Model that I can bind correctly for display purposes but I

  • 0

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; }
}
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>

<% } %>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:00:58+00:00Added an answer on May 16, 2026 at 10:00 am

    Basically you need unique ID’s for each list element. Something like

    <input id="PageContents_0_Content" name="PageContents[0].Content" value="..." />
    

    This can be done by:

    <%= Html.TextBox(string.Format("PageContents[{0}].Content", i), Model.Products[i].Id) %>
    

    This this blogpost:

    http://weblogs.asp.net/nmarun/archive/2010/03/13/asp-net-mvc-2-model-binding-for-a-collection.aspx

    This blogpost describes a much better solution:

    http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

    It uses an own extensions HTML Helper method to initialize the list bindings.

    EDIT: I misunderstood this, was focused on Collections with ASP.NET MVC:

    if I changed a pagecontents content then the posted model contains the Pages details but loses the relationship between the page and the pagecontent.

    As far as I know MVC cant restore this relationship. If Page already contains PageContent after a PostBack then everything is “fine”. Means: Your collections are posted correctly back. But MVC has no idea about you PageContent.Page reference. So you have to restore if manually.

    EDIT2:

    (BTW may be a red herring but I am using Fluent nHibernate)

    If I interpret that correctly you are using your model directly to bind your views – right? If so, I would suggest that you create ViewModels, copying everything from your model and back. This would decouple the Model from the View. Your ViewModel could provide much more information needed by your View. See MVVM Pattern.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ActiveRecord model that I would like to convert to xml, but
I have a View that can vary significantly, depending on the 'mode' a particular
Say that I have two models- Users and Accounts. Each account can have at
Specifically, I have a model that has a field like this pub_date = models.DateField(date
I am trying to use Windows Workflow and have a model that looks similar
I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a Person model that has a foreign key relationship to Book ,
I have a data model that includes common columns like addedBy, editedby (user), addedDate,
I have a rails model that validates uniqueness of 2 form values. If these
So I have a User model that :has_many other models like Documents, Videos, Posts

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.