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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:51:38+00:00 2026-05-23T01:51:38+00:00

I have a ViewModel that have a list object containing other list objects and

  • 0

I have a ViewModel that have a list object containing other list objects and want to bind that to a view.

What I have done so far is to loop through the first list like so:

    for (int i = 0; i < Model.ThFirstList.Count(); i++ )
    {
        Html.EditorFor(model => model.TheFirstList[i] , "myView")
    }

And in “myView” a loop through the other list like so:

    for (int i = 0; i < Model.TheSecondList.Count(); i++ )
    {
        %><%: Html.DropDownListFor(m => m.TheSecondList[i].ThePropertyIWantToSet, aList)%><%
    }

Everything looks fine and on the client side (I think) and the name of the select-fields is like this:

TheFirstList[0].TheSecondList[0].ThePropertyIWantToSet

When I don’t choose anything from any of the select boxes and post back the data then TheFirsList and TheSecendList objects is set. However if I choose a value in the list I get Error Code 500 back from the server. My guess is that it can’t find any matching Controller method for the request? Because of this I can’t set the ThePropertyIWantToSet property.

Can anyone help me on this?

Thanks!

  • 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-23T01:51:39+00:00Added an answer on May 23, 2026 at 1:51 am

    Instead of bothering with foreach loops etc, I would simply use a view model and editor templates. It makes life easier.

    So start with a view model:

    public class MyViewModel
    {
        public IEnumerable<Type1> TheFirstList { get; set; }
    }
    
    public class Type1
    {
        public IEnumerable<Type2> TheSecondList { get; set; }
    }
    
    public class Type2
    {
        public string ThePropertyIWantToSet { get; set; }
    
        public IEnumerable<SelectListItem> Items
        {
            get
            {
                return new[]
                {
                    new SelectListItem { Value = "1", Text = "item 1" },
                    new SelectListItem { Value = "2", Text = "item 2" },
                    new SelectListItem { Value = "3", Text = "item 3" },
                };
            }
        }
    }
    

    then a controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // filling with dummy values => those should probably
            // come from your data store
            var model = new MyViewModel
            {
                TheFirstList = Enumerable.Range(1, 2).Select(x => new Type1
                {
                    TheSecondList = Enumerable.Range(1, 3).Select(y => new Type2())
                })
            };
            return View(model);
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    Then corresponding ~/Views/Home/Index.cshtml view:

    @model MyViewModel
    @using (Html.BeginForm())
    {
        @Html.EditorFor(x => x.TheFirstList)
        <input type="submit" value="OK" />
    }
    

    The ~/Views/Home/EditorTemplates/Type1.cshtml template:

    @model Type1
    @Html.EditorFor(x => x.TheSecondList)
    

    and finally the ~/Views/Home/EditorTemplates/Type2.cshtml template:

    @model Type2
    @Html.DropDownListFor(
        x => x.ThePropertyIWantToSet, 
        new SelectList(Model.Items, "Value", "Text")
    )
    

    Now in the POST action you will get a nice and properly bound view model. Editor templates will take care of generating the correct input field names. See how easy and clean the code is. No need of writing any loops in the views, worrying about proper indexes, … just leave this to the framework.

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

Sidebar

Related Questions

Let's just say I have A ViewModel that is made up of 3 other
I have a dataset that is essentially a list of objects with a boolean
I have a ViewModel that looks like this: public class CreateReviewViewModel { public string
I have a ViewModel with a kinda complex data structure. I need my view
I have a View where I am searching my db for an object(i.e. Books)..
I have an EF4 Entity Workgroup. Below is the meta-data for that model for
I know that it seems a duplicate entry, but I red all the posts
I'm trying to implement a chart that can handle real time data, which comes
This is a wierd bug in my code and i have no idea what
I need to separate ViewModels in my MVC project from my business models (Data

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.