You can do like this, lets say you have check box like,
<input class="form-check-input" value="@dt["MNUD_TEXT"]" D="@dt["MNUD_TEXT"]" type="checkbox" name="Data" class="clsChkFromDB">
Now you have to bind change event of the checkbox using ".clsChkFromDB" selector and on change event of checkbox. you need to send ajax request which update value of checkbox in database,
$(function () {
//Bind on change event with .clsChkFromDB(Checkbox)
$("#formId").on("change", '.clsChkFromDB', function () {
updateValue(this.id, this.checked);
});
});
function updateValue(chkId, chkValue) {
try {
var strUrl = '@Url.Action("UpdateValue", "ControllerName")';
var dataObject = JSON.stringify({
"chkId": chkId,
"chkValue": chkValue
});
$.ajax({
url: strUrl,
type: 'POST',
data: dataObject,
async: true,
contentType: 'application/json',
success: function (result) {
//Do some action on success
},
complete: function (result) {
//Do some action on complate
},
error: function (err) {
console.error(err);
}
});
} catch (error) {
console.error(e);
}
}
And finally you have to write below action in your controller,
[HttpPost]
public JsonResult UpdateValue(string chkId, string chkValue)
{
dynamic result = string.Empty;
try
{
//Code to update value in DB
result = new
{
message = "",
StatusCode = HttpStatusCode.OK
};
}
catch (Exception ex)
{
result = new
{
message = ex.Message,
StatusCode = HttpStatusCode.InternalServerError
};
}
return Json(returnResult);
}