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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:56:42+00:00 2026-06-15T10:56:42+00:00

How do i display a set of checkboxes depending on the item that has

  • 0

How do i display a set of checkboxes depending on the item that has been selected in a dropdownlist?

I got a dropdownlist that contains ‘states’. The user can add cities to a state. Doing this with 2 dropdownlists (one that contains all states, and the other one containing all possible cities) would take forever for the user to add them all.

So in this case a set of checkboxes of all cities would be a more user-friendly approach. But how do i do that? And how do I get everything posted back?

I simply don’t have any idea on how to do this, so i don’t have code to show either! 🙁

[EDIT]

Part of my controller:

public ActionResult Index()
{
   IEnumerable<SelectListItem> ddlStates= dataContext.States
                              .Select(c => new SelectListItem
                              {
                                  Value = c.StateID.ToString(),
                                  Text = c.Name
                              });

   ViewBag.States = ddlStates;

   return View();
}

The view:

@model dataContext.States

@{
    ViewBag.Title = "Details";
}

@Html.DropDownList("States")

This is as far as i came.

  • 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-15T10:56:44+00:00Added an answer on June 15, 2026 at 10:56 am

    ok first of all rendering states (i assume that you have provided list of states with you’r view model):

    <select id="stateList">
    <option value="0">-------- select state ------ </option>
    @foreach(var state in Model.States)
    {
         <option value="@state.Id">
                  @state.Name
         </option>
    }
    </select>
    
    <div id="cityContainer">
    
    </div> 
    
    <input type="button" id="submitChanges"> 
    

    for getting list of cities by handling Change event:

    $("#stateList").change(function(){
          var id = $("option:selected",$(this)).attr("Id");
          $.ajax({
                 url:'@Url.Action("getStateCities","YourController")'               
                 data = {stateId = id},
                 success:function(data){
                       $.each(cities, function () {
                                var $cityList = $("#cityContainer");
                                var $checkbox= $("<input type='checkbox'></input>").attr("value",this.Id).text(this.CityName);
                                if (this.isInState)
                                 {
                                    $checkbox.attr("checke","checked");  
                                 }
                                $checkbox.appendTo($cityList);
                            });
                 } 
          })
    });
    

    and you’r controller action can look something like this :

    public JsonResult getStateCities(int stateId)
            {
                var data = _addressService.GetStateCities(Id).Select(c => new { c.CityName, c.Id  ,  isInState = isCityInEstate(cityId)});
                return new JsonResult { Data = data };
            }
    

    implementing GetStateCities and isCityInEstate is up to you.now sending form data back to controller :

      $("#submitChanges").click(function(){
           var citiesToSubmit = [];
           $("input[type=checkbox]",$("#cityContainer")).each(function(){                
                var include = $(this).prop('checked'),              
                    cityId = $(this).attr("Id"); 
                citiesToSubmit.push({CityId:cityId, Include : include});
           });
           $.ajax({
                url:'@Url.Action("SubmitCities","YourController")',
                data :{StateId = $("#estateList").attr("Id") , Cities : citiesToSubmit}
                success:function(){
                      alert('cities submitted');
                }
           });
      });
    

    and finally the controller :

       public class SubmitedCityViewModel
       {
           public int StateId {get;set;}
    
           public List<CityViewModel> Cities {get;set;}
       }
    
       public class CityViewModel
       {
           public int cityId {get;set;}
           public bool Include {get;set;}    
       }
    
    
    
       public JsonResult SubmitCities(SubmitedCityViewModel model){
               foreach(var city in model.Cities)
               { 
                   if (city.Inclue)
                   {
                         //update db to include 
                         _addressService.AddCityToState(city.Id,model.StateId);
                   }                  
                   else
                   { 
                       //update db to remove
                         _addressService.RemoveCityFromState(city.Id,model.StateId);
                   }
               } 
       } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I set display:none to a ul if only has a single child?
I putting together a page that will display a set of stored values. I
I'm trying to create a view that contains a list of checkboxes that is
I have a event model that has many invitations. Invitations are setup through checkboxes
Question: HOW DO I DISPLAY SELECTED CHECKBOXES IN MY EMAIL? I've created a survey
i need to display a list of strings to a user. the user can
I display set of images(small). I need to show the larger image(300*300) at some
I want to display result set of Years between From date - To date
I need to be able to display a set of 1200 random combinations out
I have a custom s:GridItemRenderer that contains a single s:CheckBox and is used to

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.