I have a View which is used to assign priviliges to a user. In the database i created two tables. The name of one table is MainMenuItems and the name of the second table is SubMenu Items. To assign sub menu items to a user i created a view that contains two dropdown lists. The first dropdown list displays the user Codes from the Employees table and the second dropdown list displays the main menu items from the MainMenuItems table. Now i want this view when i select a main menu item from the first DropDown list then all the sub menu items will display from the SubMenuItems table in from of a group of CheckBoxes. So first i will select an Employee Code from the first Dropdown list and select main menu item from the second dropdown list and when i select a main menu item from the second dropdown list then all the sub menu items related to the selected main menu item will display in the form of a group of CheckBoxes and i check one or more sub menu items from the list of check boxes and then press the submit button and this data will store in the database table UsersRightsAssignments. I created the complete view. I also created the checkboxes dynamically using Javascript but now i am confuse how to save the data of this View into the database table becase the first dropdown list has an attribute "asp-for" the second dropdown list contains an attribute "asp-for" but i am creating Checkboxes dynamily and they do not have the "asp-for" attribute so i confuse how can i take the values of all the selected CheckBoxes and store in the database table. Following is my comple code of View:
- <form asp-action="UserRightsAssignments" method="post">
- <div class="row">
- <div class="col-md-3">
- <div class="form-group">
- <label asp-for="UserCode" class="control-label">Select User Code</label>
- <select asp-for="UserCode" id="cmbUserCode" class="form-control" asp-items="ViewBag.UserCodes">
- </select>
- </div>
- </div>
- <div class="col-md-3">
- <div class="form-group">
- <label asp-for="MainMenuId" class="control-label">Select Main Menu ID</label>
- <select asp-for="MainMenuId" id="cmbMainMenuID" class="form-control control-size" asp-items="ViewBag.MainMenuIDs">
- </select>
- </div>
- </div>
- <div class="col-md-3">
- <div class="form-group">
- <div id="checkBoxesDiv"></div>
- </div>
- </div>
- <div> @*End of first row*@
- <div class="row">
- <div class="col-md-3">
- <div class="form-group">
- <input type="submit" value="Create" class="btn btn-primary" /> <br />
- </div>
- </div>
- </div>
- </form>
- <script type="text/javascript">
-
- $.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 <response.length; i++) {
-
- var checkbox = document.createElement('input');
-
- checkbox.type = "checkbox";
- checkbox.name = "SubMenuItems"
- checkbox.value = response[i].SubMenuName;
- checkbox.id = "CheckBox" + i;
-
- var label = document.createElement('label');
-
- label.htmlFor = response[i].SubMenuName;
-
- label.appendChild(document.createTextNode(response[i].SubMenuName));
-
- var br = document.createElement('br');
-
- checkBoxesDiv.appendChild(checkbox);
- checkBoxesDiv.appendChild(label);
- checkBoxesDiv.appendChild(br);
- }
- },
- error: function (response) {
- alert("The Sub Menu name does not display " + response.responseText);
- }
- });
- }
- </script>
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