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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:43:21+00:00 2026-05-31T06:43:21+00:00

I have a list of information sources in a database that I need to

  • 0

I have a list of information sources in a database that I need to pass to a view in MVC. I need an end user to be able to tick the sources of information that apply to their course.

I am able to successfully pass the view a list of information sources alongside check boxes using the following code.

public ViewResult CreateUpdateInfoSource(int ProgrammeId)
        {
            List<ProgInfoSourceModel> viewmodel = new List<ProgInfoSourceModel>();
            List<ProgInfoSourceDTO> myProgInfoDTOList = progInfoSourceService.AllInfoSources(); 
            if (myProgInfoDTOList.Count != 0)
            {
                foreach (var x in myProgInfoDTOList)
                {
                    ProgInfoSourceModel insert = new ProgInfoSourceModel();
                    insert.Selected = false;
                    insert.ProgrammeId = ProgrammeId;
                    insert.InfoSourceId = x.InfoSourceId;
                    insert.InfoSource = x.InfoSource;
                    insert.InfoReference = x.InfoReference;
                    insert.Rank = x.Rank;

                    viewmodel.Add(insert);
                }
            }

            return View(viewmodel);
        }

I am able to unpack this in the view just fine, however I am having real difficulty passing a the list back to my controller. I need to be able to loop through the list in my controller and see which ones do or don’t apply so I can update the database.

My model looks like this:

namespace ProgrammeSpec.MVC.Models
{
    public class ProgInfoSourceModel
    {
        [DisplayName("Selected")]
        public bool Selected { get; set; }
        [DisplayName("Programme Id")]
        public int ProgrammeId { get; set; }
        [DisplayName("Info Source Id")]
        public int InfoSourceId { get; set; }
        [DisplayName("Info Source")]
        public String InfoSource { get; set; }
        [DisplayName("Reference")]
        public String InfoReference { get; set; }
        [DisplayName("Rank")]
        public int? Rank { get; set; }
    }
}

My View looks like this:

<html>
<head>
    <title>CreateUpdateInfoSource</title>
</head>
<body>

@using (Html.BeginForm())
{
    <table>
        <tr>
            <th>
                Selected
            </th>
            <th>
                ProgrammeId
            </th>
            <th>
                InfoSourceId
            </th>
            <th>
                InfoSource
            </th>
            <th>
                InfoReference
            </th>
            <th>
                Rank
            </th>
            <th></th>
        </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.CheckBoxFor(modelItem => item.Selected)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.ProgrammeId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.InfoSourceId)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.InfoSource)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.InfoReference)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Rank)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Details", "Details", new { /* id=item.PrimaryKey */ }) |
                @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ })
            </td>
        </tr>
    }


    </table>
    <input type="submit" value="Update" />
}
</body>
</html>

and the controller that the view gets passed to looks like this: (snippet)

[HttpPost]
        public ActionResult CreateUpdateInfoSource(List<ProgInfoSourceModel> viewmodel)
        {
            if (ModelState.IsValid)
            {
                try
                {

The problem is the viewmodel is null. I understand this is probably because I’ve unpacked the list in the view so it is no longer a list but how can I access the values of the check boxes then?

The added complication is that the number of info sources will vary so I can’t use a static form or list and give each one an Id…

This must be a fairly common problem with a simple solution, but I’m an MVC novice and I don’t know how to get round this.

  • 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-31T06:43:23+00:00Added an answer on May 31, 2026 at 6:43 am

    Try using an editor template (Here’s another SO that answers that question How to create custom editor/display templates in ASP.NET MVC 3?), for your ProgInfoSourceModel and then simply use Html.EditorFor(m => m.Model) on the View.

    When you use the foreach loop, each checkbox is getting the same input name – and so is not actually submitting the correct data back.

    If you go the editor template route, and making MVC do the hard work of iterating through the IEnumerable – it will create inputs with names like ‘item[0].Selected’ – which the model binder then correctly deserialized back into a list.

    Phil Haack also blogged a fantastic walkthrough of exactly this scenario way back in 2008: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

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

Sidebar

Related Questions

I have a form with a list that shows information from a database. I
I have a dynamic list of items that will be used to POST information
I have an AdvancedDataGrid that is displaying information in a hierarchical fashion; a list
In my database I have certain tables that have confidential information. Each of these
In my database I have a list of users with information about them, and
I have to list several elements that are coming from a database source A
I have two sources of clinical procedure billing information that I have added together
I have a listview connected to a custom array adapter. This list shows information
I am trying to match information stored in a variable. I have a list
I have a master database, that has a table ( companyTable ) stores the

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.