Title: Implementing Multiselect Checkboxes in ASP.NET MVC Using AJAX
Introduction: In this article, we will explore how to implement a dynamic form with multiselect checkboxes in an ASP.NET MVC application. We'll focus on using jQuery AJAX to handle the form submission, retrieve the selected checkboxes, and save the data to the database. The provided code includes a complete example of how to create a user-friendly student data management system with the ability to select multiple languages for each student.
Prerequisites: Before proceeding, you should have a basic understanding of ASP.NET MVC, C#, jQuery, and Entity Framework.
Creating the Multiselect Checkbox Form
In the "Index.chtml" file, we have designed a form to collect student data, including a section to select multiple languages for each student using checkboxes.
<!-- Index.chtml -->
<!-- ... (existing code) ... -->
<div class="text-light text-center my-3">
<div class="d-flex justify-content-center">
<h5 class="text-warning">Languages:</h5>
<div id="languagesSection">
@for (int i = 0; i < Model.Languages.Count; i++)
{
<label>@Model.Languages[i].LanguageName</label>
@Html.CheckBoxFor(model => model.Languages[i].IsChecked)
@Html.HiddenFor(model => model.Languages[i].ID)
@Html.HiddenFor(model => model.Languages[i].LanguageName)
}
</div>
</div>
</div>
<!-- ... (existing code) ... -->
Implementing AJAX for Adding Data to Database
In the JavaScript section, we've implemented an AJAX function to handle the form submission and add student data to the database.
// AJAX for Adding Data to Database
$("#SumbitButton").click(function () {
var selected = []; // initialize array
$('div#languagesSection2 input[type=checkbox]').each(function () {
if ($(this).is(":checked")) {
selected.push($(this).attr("id"));
}
});
$("#Languages").val(selected);
var studentData = $("#studentInputForm").serialize();
if (!$('#studentInputForm').valid()) {
return false;
}
$.ajax({
url: "/Home/SaveStudent",
type: "POST",
data: studentData,
success: function (data) {
$('#studentInputForm').find(':input').val('');
$('input[type=checkbox]').prop('checked', false);
},
error: function (errormessage) {
console.log("error message");
}
});
});
Handling the AJAX Request in the Controller
In the "HomeController," we've added two action methods, one for saving student data and the other for handling the edit functionality.
public class HomeController : Controller
{
OperationRepositery repo = null;
public HomeController()
{
repo = new OperationRepositery();
}
public ActionResult Index()
{
StudentModel model = new StudentModel();
model.Languages = db.LanguagesTable.Select(lang => new LanguagesModel
{
ID = lang.ID,
LanguageName = lang.LanguagesName,
}).ToList();
return View(model);
}
// Action method for adding student data
public ActionResult SaveStudent(StudentModel model)
{
var result = repo.AddData(model);
return Json(new { result = true }, JsonRequestBehavior.AllowGet);
}
// Action method for handling the edit functionality
public ActionResult EditStudent(int ID)
{
var result = repo.EditFunction(ID);
var languageIdList = result.LanguageIds.Split(',').Select(x => Convert.ToInt32(x)).ToList();
result.Languages = db.LanguagesTable.Select(x => new LanguagesModel
{
ID = x.ID,
LanguageName = x.LanguagesName,
IsChecked = languageIdList.Contains(x.ID)
}).ToList();
return View(result);
}
}
Conclusion: In this article, we've demonstrated how to create a dynamic student data management system with multiselect checkboxes in an ASP.NET MVC application. The use of jQuery AJAX makes the form submission seamless and allows users to select multiple languages for each student. By following this guide, you can enhance your web application with more interactive features and improve the overall user experience. Happy coding!