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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:18:14+00:00 2026-06-01T08:18:14+00:00

I am developing an ASP.Net MVC 3 Web application and I am having some

  • 0

I am developing an ASP.Net MVC 3 Web application and I am having some difficulties with getting the values from a checkboxlist. I have already read most of the questions on Stackoverflow around this area, however, I am still having some issues.

I have a ViewModel

public class ViewModelCheckBox
{
    public string Id { get; set; }
    public string Name { get; set; }
    public bool Checked { get; set; }
}

Another ViewModel which use the viewmodel above

public class ViewModelAssignSubSpeciality
{
    public ListItem Item { get; set; }
    public IList<ViewModelCheckBox> SpecialityList { get; set; }
}

Then in my controller

public ActionResult AssignSubSpeciality(int id)
{
        //Get a list of all sub-specialities
        var SpecialityList = _listService.GetListItemsByID(3).ToList();

        //Get a list of sub-specialities for the the passed in id, this is either the id of a speciality or grade
        IList<RelationshipSpecialitySub> assignedSpecialities = _listService.GetAssignedSubSpecialities(id).ToList();

        var checkBoxList = new List<ViewModelCheckBox>();

        foreach (ListItem item in SpecialityList)
        {
            ViewModelCheckBox chkBox = new ViewModelCheckBox { Id = item.listItemID.ToString(), Name = item.description };

            //If sub-speciality exists in assignedSpecialities list, then make checkbox checked
            foreach (var specilaity in assignedSpecialities)
            {
                if (specilaity.subID == item.listItemID)
                {
                    chkBox.Checked = true;
                }
                else
                {
                    chkBox.Checked = false;
                }
            }

            checkBoxList.Add(chkBox);
        }

        ViewModelAssignSubSpeciality viewModel = new ViewModelAssignSubSpeciality();
        viewModel.ListItem = _listService.GetListItemByID(id);
        viewModel.SpecialityList = checkBoxList;

        return View(viewModel);
    }

The code in the controller above is getting a list of all the possible checkbox list items, then getting a list of all the previously selected checkbox list items for which it sets the checked option to true.

My View then looks like this, looping over the SpecialityList and creating a checkbox for each item, and also setting its selected value to true if needs be.

<fieldset>
<legend>Specialities</legend>

@foreach (var item in Model.SpecialityList)
{
<input type="checkbox" id="@item.Id" name="@item.Name" value="@item.Id" @(item.Checked ? "checked" : "") />
<label for="@item.Id">@item.Name</label><br />
}

<input type="submit" value="Save Changes" class="sepH_b" />                                         

My HttpPost method in my controller then looks like this

    public ActionResult AssignSubSpeciality(ViewModelAssignSubSpeciality model)
    {
        //delete all sub-specialities in tbl relationshipSpecialitySub for List
        foreach (ViewModelCheckBox item in model.SpecialityList)
        {
                //_listService.DeleteSubSpecialityFromSpeciality(item.Id);
        }

        return RedirectToAction("ListItems", new { id = model.ListItem.listID });
    }

However, when I try to get the selected checkboxes in

model.SpecialityList

It us always null. I am not sure why it doesnt contain a list of ViewModelCheckBox.

Can anyone please help me with this?

Thank you.

  • 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-01T08:18:15+00:00Added an answer on June 1, 2026 at 8:18 am

    I have an Enumerable of these in my view model

    public class CheckBoxItem
    {
       public string Code { get; set; }
       public bool IsChecked { get; set; }
       public string Label {get;set;}
    }
    

    I then use an editor template to display them on the page.

    <p class="checkbox" style="display:inline">
    <span style="margin-left:5px;">
        @Html.HiddenFor(x => x.Code)           
        @Html.CheckBoxFor(x => x.IsChecked)
    </span>
    @Html.LabelFor(x => x.IsChecked, Model.Label)
    </p>
    

    In the view I use the following to display them on the page.

    @Html.EditorFor(m => m.MyEnumerableOfCheckBoxItem)
    

    When the form is posted back the model is correctly bound.

    Hope this helps.

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

Sidebar

Related Questions

I have an Asp.Net MVC Web Application that I am developing. I have TeamCity
For the web application (ASP.NET MVC) I'm currently developing, we have the following architecture
I have started developing a full-web application by using the ASP .NET MVC 3
I'm developing a web app with asp.net mvc 3 and I have some form
I'm developing an web application using asp.net MVC and jQuery. I have in my
I'm developing a web application using ASP .NET MVC 1.0 and jQuery (including the
I'm developing an ASP.Net MVC site and on it I list some bookings from
I'm developing an ASP.NET MVC 2 application that connects to some services to do
I'm developing a ASP.NET MVC 2 web application. So far, I managed to define
I am developing an asp.net mvc 2 web application. My clients will most likely

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.