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 9015621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:47:38+00:00 2026-06-16T03:47:38+00:00

I can’t seem to understand child collections in ASP.MVC 4 and Code First binding.

  • 0

I can’t seem to understand child collections in ASP.MVC 4 and Code First binding. I always get an error that the model object is null when it comes to child collections. I can’t even add a check for if the child collection is null because the model is null.

I have verified that in the controller if I create a Batch object and add steps to it, then it will work.

I’m sure this is something simple, but I can’t figure it out.

Here are my objects:

public class Batch
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual string Details { get; set; }
    public virtual ICollection<Step> Steps { get; set; }
}
public class Step
{
    public virtual int Id { get; set; }
    public virtual string Title { get; set; }
    public virtual int Days { get; set; }
    public virtual Batch Batch { get; set; }
}

Here is my controller action:

    [Authorize]
    public ActionResult Create()
    {
        return View();
    }

Here is my view:

@model BC.Models.Batch

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Batch</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Details)
        </div>
        <div class="editor-field">
            @Html.TextAreaFor(model => model.Details)
            @Html.ValidationMessageFor(model => model.Details)
        </div>

        <div>
            <h3>Steps</h3>
            // Here is where I get a error that model is null
            @if(model.Steps != null)
            {
                foreach(var item in model.Steps)
                {
                    @Html.EditorFor(model => item)
                }
            }
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
  • 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-16T03:47:39+00:00Added an answer on June 16, 2026 at 3:47 am

    Instantiate your model and pass it to the view.

    public ActionResult Create()
            {
                var model = new Batch();
                return View(model);
            }
    

    That will solve the NullReference exception on the null model in the view.

    However, when you reach the Psot action, Steps (the collection within Batch) will likely be null. To solve this, new up the collection in the constructor like so:

    public class Batch
        {
            public Batch()
            {
                Steps = new Collection<Step>();
            }
            public virtual int Id { get; set; }
            public virtual string Title { get; set; }
            public virtual string Details { get; set; }
            public virtual ICollection<Step> Steps { get; set; }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can find why i get this error can someone help? package Android.data; public class
can't get this tutorial working in localhost: http://www.w3schools.com/jquery/tryit.asp?filename=tryjquery_ajax1 my jquery.js file has the following
Can I change the field public virtual ClassOne ClassOne { get; set; } to
Can any one tell, how to get the result of LINQ query contains group
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
Can someone please explain to me why the output from the following code is
Can I call select before recv_from on a socket that is blocking?
Can I have a project that has some parts written in c and other
Can some one confirm me that only one UIWindow instance is possible in any
Can MOSS integrate and get user profiles from multiple Active Directory and/or LDAP stores?

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.