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

  • Home
  • SEARCH
  • 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 8297071
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T15:15:53+00:00 2026-06-08T15:15:53+00:00

Apologies if this has been asked before; there are a million ways to phrase

  • 0

Apologies if this has been asked before; there are a million ways to phrase it so searching for an answer has proved difficult.

I have a viewmodel with the following properties:

public class AssignSoftwareLicenseViewModel
{
    public int LicenseId { get; set; }
    public ICollection<SelectableDeviceViewModel> Devices { get; set; }
}

A simplified version of SelectableDeviceViewModel would be this:

public class SelectableDeviceViewModel
{
    public int DeviceInstanceId { get; set; }
    public bool IsSelected { get; set; }
    public string Name { get; set; }
}

In my View, I am attempting to display a list of editable checkboxes for the Devices property, inside an input form.
Currently, my View looks like this:

@using (Html.BeginForm())
{
    @Html.HiddenFor(x => Model.LicenseId)
    <table>
        <tr>
            <th>Name</th>
            <th></th>
        </tr>
        @foreach (SelectableDeviceViewModel device in Model.Devices)
        {
            @Html.HiddenFor(x => device.DeviceInstanceId)
            <tr>
                <td>@Html.CheckBoxFor(x => device.IsSelected)</td>
                <td>@device.Name</td>
            </tr>
        }
    </table>

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

The problem is, when the model gets posted back to the controller, Devices is null.

My assumption is that this is happening because even though I’m editing its contents, the Devices property is never explicitly included in the form. I tried including it with HiddenFor, but that just resulted in the model having an empty list instead of null.

Any idea what I’m doing wrong here?

  • 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-08T15:15:55+00:00Added an answer on June 8, 2026 at 3:15 pm

    My assumption is that this is happening because even though I’m
    editing its contents, the Devices property is never explicitly
    included in the form.

    No, your assumption is wrong. The reason this doesn’t get bound properly is because your input fields doesn’t have correct names. For example they are called name="IsSelected" instead of name="Devices[0].IsSelected". Take a look at the correct wire format that needs to be used to bind to collections: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

    But why this happens?

    It happens because of the foreach loop that you used in your view. You used x => device.IsSelected as lambda expression for the checkbox but this doesn’t take into account the Devices property at all (as you can see by looking at the generated source code of your web page).

    So what should I do?

    Personally I would recommend you using editor templates as they respect the navigational context of complex properties and generate correct input names. So get rid of the entire foreach loop in your view and replace it with a single line of code:

    @Html.EditorFor(x => x.Devices)
    

    and now define a custom editor template that will automatically be rendered by ASP.NET MVC for each element of the Devices collection. Warning: the location and name of this template are very important as this works by convention: ~/Views/Shared/EditorTemplates/SelectableDeviceViewModel.cshtml:

    @model SelectableDeviceViewModel
    @Html.HiddenFor(x => x.DeviceInstanceId)
    <tr>
        <td>@Html.CheckBoxFor(x => x.IsSelected)</td>
        <td>@Html.DisplayFor(x => x.Name)</td>
    </tr>
    

    Another approach (which I don’t recommend) is to change your current ICollection in your view model to an indexed collection (such as an IList<T> or an array T[]):

    public class AssignSoftwareLicenseViewModel
    {
        public int LicenseId { get; set; }
        public IList<SelectableDeviceViewModel> Devices { get; set; }
    }
    

    and then instead of the foreach use a for loop:

    @for (var i = 0; i < Model.Devices.Count; i++)
    {
        @Html.HiddenFor(x => x.Devices[i].DeviceInstanceId)
        <tr>
            <td>@Html.CheckBoxFor(x => x.Devices[i].IsSelected)</td>
            <td>@Html.DisplayFor(x => x.Devices[i].Name</td>
        </tr>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My apologies if this question has been asked before. I can see there are
Apologies if this has been asked before (I couldn't find the answer anywhere), but
Apologies if this has been asked before. I have some data which I need
Apologies if this has been asked before but is there any way, at all,
apologies if this has been asked before but I have searched the site... Anyway,
Apologies if this is too ignorant a question or has been asked before. A
My apologies if this has been answered before or is obvious...did some searching here
Apologies if this question has been asked before, I couldn't find anything similar. I
Apologies if this has been asked already, I can't find the answer in the
I'm sure this question has been asked before, my apologies for not finding it

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.