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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:28:07+00:00 2026-06-08T09:28:07+00:00

I have a ViewModel which contains a child ViewModel. In a strongly typed Viewed

  • 0

I have a ViewModel which contains a child ViewModel. In a strongly typed Viewed of the parent, I want to RenderPartial on the child, and have results persist following POST.
But the child fields are always null.

This should work, shouldn’t it? I am fairly new to MVC, hopefully I’m missing something simple. Hopefully someone can point it out!

Thanks!

Example

ViewModels

public class EggBoxViewModel
{
    public string Brand { get; set; }
    public int Price { get; set; }
    public EggViewModel Egg { get; set; }
}

public class EggViewModel
{
    public string Size { get; set; }
    public bool IsBroken { get; set; }
}

Controller

public ActionResult Demo()
{
    EggBoxViewModel eggBox = new EggBoxViewModel();
    eggBox.Brand = "HappyEggs";
    eggBox.Price = 3;

    EggViewModel egg = new EggViewModel();
    egg.Size = "Large";
    egg.IsBroken = false;

    eggBox.Egg = egg;

    return View(eggBox);
}

[HttpPost]
public ActionResult Demo(EggBoxViewModel eggBox)
{

    // here, eggBox.Egg is null
}

Views
“Demo”

@model MvcApplication1.ViewModels.EggBoxViewModel

@using (Html.BeginForm())
{
    <h2>EggBox:</h2>
    <p>
    @Html.LabelFor(model => model.Brand)
    @Html.EditorFor(model => model.Brand)
    </p>
    <p>
    @Html.LabelFor(model => model.Price)
    @Html.EditorFor(model => model.Price)
    </p>

    <p>
        @{Html.RenderPartial("_Egg", Model.Egg);}
    </p>

    <input type="submit" value="Submit" />
}

“_Egg” (Partial)

@model MvcApplication1.ViewModels.EggViewModel

<h2>Egg</h2>
<p>
@Html.LabelFor(model => model.Size)
@Html.EditorFor(model => model.Size)
</p>
<p>
@Html.LabelFor(model => model.IsBroken)
@Html.CheckBoxFor(model => model.IsBroken)
</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-06-08T09:28:10+00:00Added an answer on June 8, 2026 at 9:28 am

    Use an Editor Template, in most cases they’re better for rendering child objects or collections. In your Views\Shared folder create a new folder called EditorTemplates if you don’t already have one. Add a new partial view called EggViewModel and use the same code as in your partial view. This is the bit of magic that renders and names all your fields correctly. It will also handle a collection of EggViewModels without a for each loop as the Editor template will automatically render all the items passed in a collection:

    @model MvcApplication1.ViewModels.EggViewModel
    
    <h2>Egg</h2>
    <p>
    @Html.LabelFor(model => model.Size)
    @Html.EditorFor(model => model.Size)
    </p>
    <p>
    @Html.LabelFor(model => model.IsBroken)
    @Html.CheckBoxFor(model => model.IsBroken)
    </p>
    

    Then in your Demo View use your new Editor Template instead of the partial:

    <p>
        @Html.EditorFor(x => x.Egg)
    </p>
    

    Here are the fields that are rendered:

    Rendered fields

    In the post back you can that the EggViewModel is now part of the EggBoxViewModel:

    Also in the ModelState you can see that the Egg fields are prefixed with the property name used in the EggBoxViewModel which makes them a subset of EggBox.

    And…the great thing is that if you want to have a collection of Eggs in your EggBox it’s really easy…just make your Egg property on EggBoxViewModel a collection:

    public class EggBoxViewModel
    {
        public string Brand { get; set; }
        public int Price { get; set; }
        public ICollection<EggViewModel> Eggs { get; set; }
    }
    

    Add a second egg:

    public ActionResult Demo()
    {
        EggBoxViewModel eggBox = new EggBoxViewModel();
        eggBox.Brand = "HappyEggs";
        eggBox.Price = 3;
    
        EggViewModel egg = new EggViewModel();
        egg.Size = "Large";
        egg.IsBroken = false;
    
        EggViewModel egg2 = new EggViewModel();
        egg2.Size = "Medium";
        egg2.IsBroken = false;
    
        eggBox.Eggs = new List<EggViewModel>();
    
        eggBox.Eggs.Add(egg);
        eggBox.Eggs.Add(egg2); 
    
        return View(eggBox);
    }
    

    Change your View to render x.Eggs instead of x.Egg:

    <p>
        @Html.EditorFor(x => x.Eggs)
    </p>
    

    Then on post back you’ll see there are 2 Eggs posted back:

    enter image description here

    The field names have automatically been indexed and named to create a collection of Eggs:

    enter image description here

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

Sidebar

Related Questions

I have following ViewModel (TripSearchView) which contains IEnumerable(of AffiliateComponentTypeView) property, I have to populate
I have a view that is strongly typed to a view model which contains
I have this block of xaml and I made a ViewModel which contains a
I have a Usercontrol(TabUserControl) which contains a TabControl. The Viewmodel of that UserControl loads
I have the following ViewModel, which is the base class of other ViewModels that
I have the following view model which contains an array of elements function ReservationsViewModel()
I have an object (Object1), which contains a child object (Child). Thus, you would
I have a ViewModel which contains an ObservableCollection of booleans. And I have a
I have a ViewModel which contains a List of my Model, like so: public
I have a ViewModel which contains a collection of type of my Model, like

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.