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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:05:22+00:00 2026-05-31T23:05:22+00:00

I have a view model that contains a Product class type and an IEnumerable<

  • 0

I have a view model that contains a Product class type and an IEnumerable< Product > type. On the post the first level product object comes back binded from the viewmodel whereas the product enum is coming back null.

Why is the IEnumerable< Prouduct> property not getting binded to my view model per the post? Thx!

Models:

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public decimal Price { get; set; }
}

public class ProductIndexViewModel
{
    public Product NewProduct { get; set; }
    public IEnumerable<Product> Products { get; set; }
}

public class BoringStoreContext 
{
        public BoringStoreContext()
         {
             Products = new List<Product>();
             Products.Add(new Product() { ID = 1, Name = "Sure", Price = (decimal)(1.10) });
             Products.Add(new Product() { ID = 2, Name = "Sure2", Price = (decimal)(2.10) });
         }
         public List<Product> Products {get; set;}
}

View:

@model ProductIndexViewModel

@using (@Html.BeginForm())
{
    <div>
        @Html.LabelFor(model => model.NewProduct.Name)
        @Html.EditorFor(model => model.NewProduct.Name)
    </div>
    <div>
        @Html.LabelFor(model => model.NewProduct.Price)
        @Html.EditorFor(model => model.NewProduct.Price)
    </div>
    <div>
        <input type="submit" value="Add Product" />
    </div>

        foreach (var item in Model.Products)
        { 
         <div>
        @Html.LabelFor(model => item.ID)
        @Html.EditorFor(model => item.ID)
    </div>
    <div>
        @Html.LabelFor(model => item.Name)
        @Html.EditorFor(model => item.Name)
    </div>
    <div>
        @Html.LabelFor(model => item.Price)
        @Html.EditorFor(model => item.Price)
    </div>

        }
       }

Controller:

public class HomeController : Controller
{
    BoringStoreContext db = new BoringStoreContext();

    public ActionResult Index()
    {
        ProductIndexViewModel viewModel = new ProductIndexViewModel
        {
            NewProduct = new Product(),
            Products = db.Products
        };
        return View(viewModel);
    }

    [HttpPost]
    public ActionResult Index(ProductIndexViewModel viewModel)
    {
        // ???? viewModel.Products is NULL here
        // ???? viewModel.NewProduct comes back fine

        return View();
    }
  • 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-31T23:05:23+00:00Added an answer on May 31, 2026 at 11:05 pm

    You are not using your lambda expression properly. You need to be accessing the Products list through the model. Try doing it like this:

    @count = 0
    foreach (var item in Model.Products)
        { 
         <div>
        @Html.LabelFor(model => model.Products[count].ID)
        @Html.EditorFor(model => model.Products[count].ID)
    </div>
    <div>
        @Html.LabelFor(model => model.Products[count].Name)
        @Html.EditorFor(model => model.Products[count].Name)
    </div>
    <div>
        @Html.LabelFor(model => model.Products[count].Price)
        @Html.EditorFor(model => model.Products[count].Price)
    </div>
    @count++
        }
    

    Edit

    Controller:

    BoringStoreContext db = new BoringStoreContext();
    
        public ActionResult Index()
        {
            ProductIndexViewModel viewModel = new ProductIndexViewModel
            {
                NewProduct = new Product(),
                Products = db.Products
            };
            return View(viewModel);
        }
    
        [HttpPost]
        public ActionResult Index(ProductIndexViewModel viewModel)
        {
            // work with view model
    
            return View();
        }
    

    Model

    public class Product
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
    }
    
    public class ProductIndexViewModel
    {
        public Product NewProduct { get; set; }
        public List<Product> Products { get; set; }
    }
    
    public class BoringStoreContext
    {
        public BoringStoreContext()
        {
            Products = new List<Product>();
            Products.Add(new Product() { ID = 1, Name = "Sure", Price = (decimal)(1.10) });
            Products.Add(new Product() { ID = 2, Name = "Sure2", Price = (decimal)(2.10) });
        }
        public List<Product> Products { get; set; }
    }
    

    View:

    @model Models.ProductIndexViewModel
    
    
    @using (@Html.BeginForm())
    {
    <div>
        @Html.LabelFor(model => model.NewProduct.Name)
        @Html.EditorFor(model => model.NewProduct.Name)
    </div>
    <div>
        @Html.LabelFor(model => model.NewProduct.Price)
        @Html.EditorFor(model => model.NewProduct.Price)
    </div>
    
    
    for (int count = 0; count < Model.Products.Count; count++ )
    { 
        <div>
        @Html.LabelFor(model => model.Products[count].ID)
        @Html.EditorFor(model => model.Products[count].ID)
        </div>
        <div>
        @Html.LabelFor(model => model.Products[count].Name)
        @Html.EditorFor(model => model.Products[count].Name)
        </div>
        <div>
        @Html.LabelFor(model => model.Products[count].Price)
        @Html.EditorFor(model => model.Products[count].Price)
        </div>
    }
    
    <div>
        <input type="submit" value="Add Product" />
    </div>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a view model that contains a fill object and an rx object.
I have a ViewModel that contains a Product type and an IEnumerable< Product >
I have a viewmodel that contains a product and SelectList of categories. public class
I have a form that contains the data to create a Product object. The
I have a Quiz.Component.Product.Type and a view that provides the component. when the Create
I have a model that contains an IEnumerable of a list of custom objects.
I have a Model that contains a List of a custom type. I want
I have a ViewModel class that contains a list of points, and I am
if i have created a view model and have a partial form that is
I have a Java project that I'm trying to implement with a model-view-controller design.

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.