@if (Model.Competencies != null && Model.Competencies.Any())
{
for (int i = 0; i < Model.Competencies.Count(); i++)
{
@Html.DropDownListFor(model => Model.Competencies[i].Compentency_Group, new SelectList(HRCompentencyTools.Models.ModelCompentencyAssessment.GetCompetencyGroups(), "Value", "Text", Model.Competencies[i].Compentency_Group),
new { @class = "form-control competency-edit "})
@Html.DropDownListFor(model => Model.Competencies[i].Competency, new SelectList(HRCompentencyTools.Models.ModelCompentencyAssessment.GetBankCompetencies(), "Value", "Text", Model.Competencies[i].Competency),
new { @class = "select js-example-basic-single form-control competency-edit"})
@Html.DropDownListFor(model => model.Competencies[i].Required_Score, new SelectList(Enumerable.Range(1, 5).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString(), }), "Value", "Text"), Model.Competencies[i].Required_Score,
new { @class = "form-control competency-edit",})
}
}
How do I make it so that when I click the radio button yes he is disabled, but when I click no the data row is deleted all and there is an add button to add a new data row and dropdownlist according to the model and submit button
Aman GuptaPosted Sep 24, 2024, 6:45 AM
Hi DZTM,
To achieve the functionality you're describing in an MVC Razor view, we can break it down into several steps:
Radio Button Logic: You want to enable or disable the row based on the radio button selection.
Row Deletion: If the "No" radio button is selected, delete the row.
Add New Data Row: Have an "Add" button to dynamically add new rows with the dropdown lists.
Submit Button: Ensure there's a button to submit the form data.
Below is an example of how you can implement this using JavaScript and Razor:
1. Radio Button Logic
You need radio buttons for "Yes" and "No" for each competency row, which will either disable the row or delete it.
2. Row Deletion:
When the "No" radio button is selected, remove the row using JavaScript.
3. Add New Row Logic:
Create a button that dynamically adds a new row with dropdowns.
Updated Code Example:
4. JavaScript for Row Logic:
-
- When you click the "Yes" button, it triggers the
- When you click the "No" button, it triggers the
-
- The
-
- The form will submit the current state of the rows, including any that were dynamically added or modified.
Adjusting for Model Binding:Radio Buttons:
disableRowfunction, which disables the dropdowns and input elements in that row.removeRowfunction, which removes the entire row.Add New Row:
addRowfunction clones the last row, clears its values, and appends it to the table.Submit Button:
Make sure your model binding works correctly for dynamic rows. You may need to adjust the name attributes or indexes dynamically in JavaScript if the number of rows is variable, to ensure the model binder in MVC can correctly bind the submitted data to your
Model.Competencies.This solution dynamically handles adding and removing rows, as well as enabling/disabling them based on radio button clicks.
pavinaPosted Sep 23, 2024, 12:47 PM
Razor Code for Dropdowns and Radio Buttons
JavaScript for Managing Rows
Each row has dropdowns for competency details and radio buttons for interaction. If you click "Yes," the dropdowns are disabled; if "No," the whole row gets removed. Clicking "Add Competency" duplicates the last row, clears the inputs, and makes them ready for new entries. Each row also has a "Remove" button to delete it. This setup keeps everything organized and lets users easily manage competencies. Let me know if you need any tweaks!
yash patelPosted Sep 23, 2024, 12:02 PM
use the below code for creating dropdown, raido button and add new row
@if (Model.Competencies != null && Model.Competencies.Any())
{
for (int i = 0; i < Model.Competencies.Count(); i++)
{
@Html.DropDownListFor(model => Model.Competencies[i].Compentency_Group,
new SelectList(HRCompentencyTools.Models.ModelCompentencyAssessment.GetCompetencyGroups(), "Value", "Text",
Model.Competencies[i].Compentency_Group),
new { @class = "form-control competency-edit", disabled = "disabled" })
@Html.DropDownListFor(model => Model.Competencies[i].Competency,
new SelectList(HRCompentencyTools.Models.ModelCompentencyAssessment.GetBankCompetencies(), "Value", "Text",
Model.Competencies[i].Competency),
new { @class = "select js-example-basic-single form-control competency-edit", disabled = "disabled" })
@Html.DropDownListFor(model => model.Competencies[i].Required_Score,
new SelectList(Enumerable.Range(1, 5).Select(x => new SelectListItem { Value = x.ToString(), Text = x.ToString(), }),
"Value", "Text"),
Model.Competencies[i].Required_Score,
new { @class = "form-control competency-edit", disabled = "disabled" })
Yes
No
}
}
js code for add row , delete row
function toggleRow(radioBtn, isYes) {
var row = $(radioBtn).closest('tr');
if (isYes) {
// Disable all inputs in this row
row.find('select').prop('disabled', true);
} else {
// Remove the row if "No" is clicked
row.remove();
}
}
function addRow() {
// Clone the last row and clear its values
var lastRow = $('.competencyRow:last');
var newRow = lastRow.clone();
// Reset the inputs in the cloned row
newRow.find('select').val('');
newRow.find('input[type="radio"]').prop('checked', false);
newRow.find('select').prop('disabled', false);
// Append the new row to the table
lastRow.after(newRow);
}
function Remove(button) {
// Remove the row on "Remove" button click
$(button).closest('tr').remove();
}