Binding a checkbox with ViewModel without using Razor (Asp.net core MVC)

0

I'm having problems with Razor ... I need to create a checkbox field and do the binding with my viewModel using pure html. I'm not sure how to do this, but I'm not sure how to do this. (fields are being created dynamically ...)

<div class="checkbox-custom checkbox-default">
    <input type="checkbox" name="PessoasContatosViewModel[@i].ContatoPrincipal" class="ckb-contatoPrincipal" autocomplete="off" />
    <label asp-for="PessoasContatosViewModel[i].ContatoPrincipal" class="ckb-contatoPrincipal">Contato Principal</label>
</div>

I was able to resolve the problem by validating the checked property, but is it correct?

<div class="col-md-2">
    <label class="control-label">&nbsp;</label>
    <div class="checkbox-custom checkbox-default">
        <input type="checkbox" name="PessoasContatosViewModel[@i].ContatoPrincipal" value="true" checked ="@(Model.PessoasContatosViewModel[i].ContatoPrincipal)" class="ckb-contatoPrincipal" autocomplete="off" />
        <label asp-for="PessoasContatosViewModel[i].ContatoPrincipal" class="ckb-contatoPrincipal">Contato Principal</label>
    </div>
</div>
    
asked by anonymous 06.10.2018 / 23:40

1 answer

0

You should put a property for it checked or not, because it identifies the changes as if it were an "on" event.

Add this part to your checkbox:

value="true" checked

As you mentioned, you can try to use the razor to define whether it is actually checked or not, as follows:

checked ="@(Model.PessoasContatosViewModel[i].ContatoPrincipal)"

Any questions and if I can help in any other way, just let me know! :)

    
09.10.2018 / 13:46