Skip to content
Loading
CheckBox Group in MVC Core  
  • class="col-md-3">  
  • class="form-group">  
  • "checkBoxesDiv">  
  •   
  •   
  •  @*End of first row*@  
  • class="row">  
  • class="col-md-3">  
  • class="form-group">  
  • "submit" value="Create" class="btn btn-primary" />   
  •   
  •   
  •   
  •   
  • "text/javascript">  
  • //Retrieve a List of Sub Menu Item Names by Main Menu Item ID  
  • $.ajax({  
  • url: "/Administration/RetrieveSubMenuNameByID",  
  • type: "GET",  
  • dataType: "json",  
  • data: { "mainMenuID": mainMenuID },  
  • success: function (response) {  
  • $("#checkBoxesDiv").empty();  
  • var checkBoxesDiv = document.getElementById("checkBoxesDiv");  
  • for (var i = 0; i 
  • //Creating checkbox elements according to the retrieved sub menu items  
  • var checkbox = document.createElement('input');  
  • //Assigning the attributes to created checkbox  
  • checkbox.type = "checkbox";  
  • checkbox.name = "SubMenuItems" //The name of all CheckBoxes is same because it is group  
  • checkbox.value = response[i].SubMenuName;  
  • checkbox.id = "CheckBox" + i;  
  • //Creating label for checkboxe  
  • var label = document.createElement('label');  
  • //Assigning attributes for the created label tag  
  • label.htmlFor = response[i].SubMenuName;  
  • //Appending the created text to the created label tag  
  • label.appendChild(document.createTextNode(response[i].SubMenuName));  
  • //Create the 
     tag and use after each Checkbox Control
      
  • var br = document.createElement('br');  
  • //Appending the checkbox and label to div  
  • checkBoxesDiv.appendChild(checkbox);  
  • checkBoxesDiv.appendChild(label);  
  • checkBoxesDiv.appendChild(br);  
  • }  
  • },  
  • error: function (response) {  
  • alert("The Sub Menu name does not display " + response.responseText);  
  • }  
  • });  
  • }  
  •   
  • How to store the above form data in the database table that contains the following three fields:
     
    UserCode
    MainMenuId
    SubMenuId
     
    The UserCode and MainMenuId has one to many relation with SubMenuId because for a single UserCode and for a single MainMenuId there is one or more selected values of SubMenuId which is checked from the above list of CheckBoxes.
     
    Your help will be highly apprecitres because this is part of my project. Thanks in advance
    Please share your code in MVC Core

    1 Reply

    Know the answer? Post it — somebody with the same question will find it here.