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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T02:24:28+00:00 2026-06-19T02:24:28+00:00

I have a viewmodel which contains an Episode and a boolean watched: public class

  • 0

I have a viewmodel which contains an Episode and a boolean “watched”:

public class WatchedEpisodeViewModel
{
    public Episode Episode { get; set; }
    public bool Watched { get; set; }
}

I have a list of these bound to my view:

@model IEnumerable<Seriebeheer.Web.Models.WatchedEpisodeViewModel>

I’m listing this information:

         @foreach (var episode in Model)
         {
             <tr>
                 <td>@episode.Episode.ID</td>
                 <td>@episode.Episode.Name</td>
                 <td>@episode.Episode.Date.Value.ToString("dd/MM/yyyy")</td>
                 <td><input type="checkbox" value="@episode.Episode.ID" checked="@episode.Watched"/></td>
             </tr>
         }

The user can mark the checkboxes and press a button to submit the info. I’d like to get to see in my controller which checkboxes are marked and which are not.

What’s the best way to achieve this?

EDIT:

[HttpPost]
public ActionResult Checkin(IEnumerable<WatchedEpisodeViewModel> episodes)
{
    foreach (WatchedEpisodeViewModel episode in episodes) <-- nullreference exception
    {
        if (episode.Watched)
        {
            // test
        }
    }
    return RedirectToAction("Index", "Home");
}
  • 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-19T02:24:30+00:00Added an answer on June 19, 2026 at 2:24 am

    The problem is that you’re using a foreach loop and not a for loop. You need to use a for loop because that will give the correct index to each of the fields to allow the model binder to do it’s stuff.

    Another of your problems, is you need to do CheckBoxFor on the Watched bool in your model.

    Your final problem, is you haven’t got any HiddenFors for your Episode fields, so when you post, you won’t know what’s been watched as the Episode on each of the items in the model will be null

    Try this instead:

    @for (int i = 0; i < Model.Count(); i++)
    {
        <tr>
            <td>
                @Html.HiddenFor(m => m[i].Episode.ID)
                @Model[i].Episode.ID
            </td>
            <td>
                @Html.HiddenFor(m => m[i].Episode.Name)
                @Model[i].Episode.Name
            </td>
            <td>
                @Html.HiddenFor(m => m[i].Episode.Date)
                @Model[i].Episode.Date.Value.ToString("dd/MM/yyyy")
            </td>
            <td>
                @Html.CheckBoxFor(m => m[i].Watched)
            </td>
        </tr>
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following ViewModel public class NewCustomerViewModel { public long CustomerId {get;set;} public
I have a ViewModel which contains a List of my Model, like so: public
I have a Home ViewModel class which contains others class: public class HomeVM {
Imagine I have this class: Class Foo { public Bar b1 { get; set;
I have ViewModel class as follows: public class ListViewModel { public ObservableCollection<InfoItem> List {
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
In my ViewModel I have a property, which is a list of models: private
I have a ViewModel that I created with knockout which contains all the info

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.