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

  • Home
  • SEARCH
  • 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 8216387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T12:06:28+00:00 2026-06-07T12:06:28+00:00

I have two models: OuterModel and InnerModel. There is a one to many relationship

  • 0

I have two models: OuterModel and InnerModel. There is a one to many relationship between OuterModel and InnerModel, respectively. To clarify my question, my model is of type IEnumerable<OuterModel>. I’m passing a random number of OuterModels to the view and the user creates any number of InnerModels for each OuterModel. Then on submission, I want the controller to receive the list of OuterModels so that the InnerModels can be added to the database to their intended OuterModels.

I believe I have the naming convention correct to make use of MVC’s built in model binding. Here’s what that looks like:

OuterModel[i].InnerModel[j].Property

My problem is, I don’t really know how to get a list of OuterModels passed to the controller. Here’s what I’ve tried in my View:

    @model IEnumerable<OuterModel>

    @using (Html.BeginForm("Create", "Controller", new { OuterModels = Model }, FormMethod.Post))
    {
       //Code to create the InnerModels here
    }

And here’s what I have in my Controller:

    [HttpPost]
    public ActionResult Create(IEnumerable<OuterModel> OuterModels, FormCollection fc)
    {
       String[] keys = fc.AllKeys;
       if(ModelState.IsValid){
          //Add to db
       }
    }

Keys shows that all of my properties are following the naming convention that I specified earlier, but ModelState.IsValid is returning false. It shows that OuterModels’ count is 0.

Even though I’m telling the form to submit OuterModels = Model before any InnerModels are created, you would think there would still be data in OuterModels considering it’s passed to the view. I am really tired today, so I’m guessing I’m looking over one (or many) small detail(s). Any suggestions?

–EDIT–

Passing a list of OuterModels to the controller may not be the best approach. If anybody has a better suggestion, please share.

  • 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-06-07T12:06:30+00:00Added an answer on June 7, 2026 at 12:06 pm

    As long as indexes are used properly, then this should not be an issue. Here is how I would envision the form names.

    Model[0].foo

    Model[0].Inner[0].bar

    Model[0].Inner[1].bar

    Where outer model has a property called foo and
    Outer model has a property called inner which is a collection of inner objects. Inner object has a property called bar. If your form is rendered with the correct indexes then the model binding should work. Things can get tricky if form fields are generated client side. I recommended going back to server in order to manipulate the model. There are some extra round trips, but you can make them via Ajax request.

    Here are some more details in a more fleshed out example.

    public class InnerModel{
        public string Name{get; set;}
    }
    
    public class OuterModel{
       public List<InnerModel> InnerModels{get; set;}
       public string Name{get; set;}
    }
    

    Here is what I would envision my view would look like:

    @model IEnumerable<OuterModel>
    
    <ul>
      @{int i = 0;}
      @foreach(var item in Model){
         <li>
            Outer Name : @Html.TextBoxFor(m=>Model[i].Name)
            <br />
            @{int j = 0;}
            <ul>
                @foreach(var innerItem in Model[i].InnerModels){
                   <li>Inner Name : @Html.TextBoxFor(m=> Model[i].InnerModels[j].Name)</li>
                   j++;
    
                }
            </ul> 
            i++;
         </li>
      }
    </ul>
    

    If this is wrapped in a form— and the controller action looks like this:

    public ActionResult Action(List<OuterModel> model){
    }
    

    then I would think model would be populated correctly.

    I noticed your form.. it doesn’t look right to me… I wouldn’t think that the passing the OuterModels like that is going to work– although frankly I might be wrong.

    @using (Html.BeginForm("Create", "Controller", new { OuterModels = Model }, FormMethod.Post))
    {
       //Code to create the InnerModels here
    }
    

    Here is an example I did for the class I teach.. that definitely works..

    public class Author
    {
        public string Name { get; set; }
    }
    
    public class Book
    {
        public string Name { get; set; }
    
        public List<Author> Authors { get; set; }
    }
    

    Controller:

    public class BookController : Controller
    {
    
        public static List<Book> _model = null;
    
        public List<Book> Model
        {
            get
            {
                if (_model == null)
                {
                    _model = new List<Book>
                    {
                        new Book{
                            Name = "Go Dog Go", 
                            Authors = new List<Author>{
                                new Author{Name = "Dr. Seuss"}
                            }},
                        new Book{
                            Name = "All the Presidents Men", 
                            Authors = new List<Author>{
                                new Author{Name = "Woodward"},
                                new Author{Name = "Bernstein"}
                            }},
                        new Book{
                            Name = "Pro ASP.NET MVC Framework", 
                            Authors = new List<Author>{
                                new Author{Name = "Sanderson"},
                                new Author{Name = "Stewart"},
                                new Author {Name = "Freeman"}
                            }}
                    };
                }
                return _model;
            }
        }
    
        public ActionResult Index()
        {
            return View(Model);
        }
    
        public ActionResult Edit()
        {
            return View(Model);
        }
    
        [HttpPost]
        public ActionResult Edit(List<Book> books)
        {
            _model = books;
            return RedirectToAction("Index");
            //return View(books);
        }
    
    }
    

    and View:

    @model List<AmazonWeb.Models.Book>
    @{
        ViewBag.Title = "Index";
    }
    
    <div class="content">
    
    @Html.ActionLink("Index", "Index")
    
    @using (Html.BeginForm())
    {
      <input type="submit" value="save" />
    <ul class="book-list">
    
    @for (var i = 0; i < Model.Count; i++ )
    {
        <li>
            <label>Book Name</label> : @Html.TextBoxFor(m => Model[i].Name)
            <ul>
                @for (var j = 0; j < Model[i].Authors.Count; j++ )
                {
                    <li><label>Author Name</label> : @Html.TextBoxFor(m => Model[i].Authors[j].Name)</li>
                }
            </ul>
        </li>
    }
    </ul>
        <input type="submit" value="save" />
    }
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models, one is Group, the other Item. A group has many
I have two models and controllers: Snippets, and Tags. There is a belongs_to relationship,
I have two Models, Programme and Event, a programme has many events. I need
I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two models, A and B, and one light, L. I would like
I have two models Company and Role related by has_many and belongs_to association respectively.
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have two models: PostType1 , PostType1 . class PostType1(models.Model): ... created_date = models.DateTimeField(_('created
I have two models like this: class Store(models.Model): name = models.CharField(max_length=255) class Order(models.Model): store

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.