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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:58:16+00:00 2026-05-20T08:58:16+00:00

I am trying to bind to a Model that has a collection property, specifically

  • 0

I am trying to bind to a Model that has a collection property, specifically a List. For the purposes of this example, this represents a list of user roles:

public class RolesModel
{
   private List<SelectListItem> _Roles = null;
   public string Name { get; set; }
   public List<SelectListItem> Roles
   {
      get {
        if (_Roles == null) { _Roles = new List<SelectListItem>(); }
        return _Roles;
      }
      set { _Roles = value; }
   }
}

I am binding this to a strongly-typed view via the following Controller:

public class TestController : Controller
{
   RolesModel myModel = new RolesModel();

   [HttpGet]
   public ActionResult Edit()
   {
      myModel.Name    = "Joe Bloggs";
      myModel.Roles = new List<SelectListItem>
      {
         new SelectListItem { Value = "1", Text = "Member", Selected = true },
         new SelectListItem { Value = "2", Text = "Manager", Selected = true },
         new SelectListItem { Value = "3", Text = "Administrator", Selected = false }
      };

      return View(myModel);
   }


   [HttpPost]
   public ActionResult Edit(RolesModel m)
   {
      // !!! m.Roles is always empty !!!
      return View("Results", m);
   }
}

This then invokes the following view:

@model MyProject.WebUI.Models.RolesModel
@using (Html.BeginForm())
{
   <p>
      @Html.LabelFor(m => m.Name)
      @Html.EditorFor(m => m.Name)
   </p>                              
   <div>
      @Html.EditorFor(m => m.Roles, "CheckBoxList")
   </div>                              
   <p>
      <input type="submit" value="Save" />
   </p>
}

Note the template specific call to my custom editor template in ‘/Views/Shared/EditorTemplates/CheckBoxList.cshtml’ this looks like this:

@model List<System.Web.Mvc.SelectListItem>

<h3>Type: @Html.LabelFor(m => m)</h3>
<ul>
   @for (int i = 0; i < Model.Count; i++)
   {
      <li>
         @Html.CheckBoxFor(m => m[i].Selected)
         @Html.LabelFor(m => m[i].Selected, Model[i].Text)
         @Html.HiddenFor(m => m[i].Value)
      </li>
   }
</ul>

The idea being that each SelectListItem is represented by the Html rendered by the loop.

The first part of the process appears to work correctly, The form is presented as expected and you can update the ‘Name’ text box and the check/uncheck the checkboxes.

The problem is that when the form is posted back to the controller, the Roles collection is never populated.

I’m new to MVC and thought that the framework actually re-constructed the model data from the post via the enforced form element naming convention. I’m obviously missing an important point and I’m hoping someone can point me in the right direction.

Thanks, and apologies for the long post.

  • 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-20T08:58:17+00:00Added an answer on May 20, 2026 at 8:58 am

    Here’s how you could proceed:

    @model MyProject.WebUI.Models.RolesModel
    @using (Html.BeginForm())
    {
       <p>
          @Html.LabelFor(m => m.Name)
          @Html.EditorFor(m => m.Name)
       </p>                              
       <div>
          <ul>
              @Html.EditorFor(m => m.Roles)
          </ul>
       </div>                              
       <p>
          <input type="submit" value="Save" />
       </p>
    }
    

    and inside the EditorTemplate (/Views/Shared/EditorTemplates/SelectListItem.cshtml):

    @model System.Web.Mvc.SelectListItem
    <h3>Type: @Html.LabelFor(m => m)</h3>
    <li>
        @Html.CheckBoxFor(m => m.Selected)
        @Html.LabelFor(m => m.Selected, Model.Text)
        @Html.HiddenFor(m => m.Value)
    </li>
    

    Notice the simplification of the editor template. It no longer takes a List<SelectListItem> as model but simply a SelectListItem. It will automatically be invoked for each element of the Roles collection so that you don’t need to write any loops. Just follow the conventions.

    I would also simplify your view model like this:

    public class RolesModel
    {
        public string Name { get; set; }
        public IEnumerable<SelectListItem> Roles { get; set; }
    }
    

    and your controller:

    public class TestController : Controller
    {
        public ActionResult Edit()
        {
            var myModel = new RolesModel
            {
                Name = "Joe Bloggs",
                Roles = new[]
                {
                    new SelectListItem { Value = "1", Text = "Member", Selected = true },
                    new SelectListItem { Value = "2", Text = "Manager", Selected = true },
                    new SelectListItem { Value = "3", Text = "Administrator", Selected = false }
                }
            };
            return View(myModel);
        }
    
        [HttpPost]
        public ActionResult Edit(RolesModel m)
        {
            // m.Roles should be correctly bound
            return View("Results", m);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is the exception that I'm getting when I'm trying to bind to a
I'm trying to bind a view model property to the 'SelectedItem' attribute of a
I am trying to model the layout that is displayed in this image .
I'm trying to bind a list of custom objects to a WPF Image like
I'm trying to bind a list of integers into an SQLTemplate IN clause like
I have a custom class Contact . I am trying to bind a List<Contact>
i am trying to use this code to bind my asp.net menu control to
Trying to get UpdateModel to work for my User. The User class has basic
I'm trying to bind the following shortcut: Ctrl + W to close tabs How
I'm trying to bind controls in a WPF form to an interface and I

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.