0

i got a checkbox in my view and i want to set that as checked from mvc controller. Here is my checkbox

<td>
<input type="checkbox" name="filtroReperibilita" id="filtroReperibilita" style="height:25px; width:25px; margin-left:5px; margin-top:5px;" />
</td>

I have the value that i want to set to it in session. can you help me?

CSharper
  • 21
  • 9

1 Answers1

2

Solution 1, If using ASP-Core:

use asp-for Tag Helper and set property true in the controller.

public class MyModel
{
    public bool FiltroReperibilita { get; set; }
}

<td>
   <input class="form-check-input" asp-for="FiltroReperibilita"/> @Html.DisplayNameFor(model => model.FiltroReperibilita)
</td>

For more information about Tag Helpers see Microsoft document.

Solution 2:

Consider the previous model and use this code:

<input type="checkbox" name="filtroReperibilita" id="filtroReperibilita" class="form-check-input" @(Model.FiltroReperibilita ? "checked" : string.Empty) />
Hamid Molareza
  • 190
  • 2
  • 9