TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Adalat Khan
649
1.5k
867k
CheckBox Group in MVC Core
May 17 2019 3:50 AM
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"
>
//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 <response.length; 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 <br> 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);
}
});
}
</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
Reply
Answers (
1
)
XAMARIN and MYSQL
How to generate excel format using json data in web API