samedi 4 décembre 2021

Is it possible assign the checked state to the value of the current checkbox in razor form without onclick method?

I have an ASP.Net Core MVC Web Application project. I want to send the checkbox's checked state as the value to the model's related parameter on form submit. I have seen so many examples that using jquery to get/set the value of the checkbox via onchange methods.

What I wonder is that is it possible to assign the current checkbox's checked state to the value in a similar way to the code below.(it doesn't work)

<input type="checkbox" id="IsActive" name="DTO.IsActive" checked="@Model.IsActiveTournament" value="(this.checked)" 

One of the simplest working examples I found is the following but it returns false if the checkbox is checked by default and its state is not changed.

<input type="checkbox" id="IsActive" name="DTO.IsActive" checked="@Model.IsActive" onchange="if(this.checked) this.value='true'; else this.value='false';" >

I know that there are many ways to achieve what I want but I want the cleanest solution.

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire