How to show checkbox as checked in update using MVC

Viewed 25

I have an attribute called IsActive and it's bool. I use checkbox to get this value. (If checkbox is checked it means that record is active but if it's not it is not active. When user wants to update the record, I get the record with a method called getrecordbyId. Everything works fine I get all the values true but if the record is active I want to show user that the checkbox as checked in update view. What should I add to show the active record as checked on my checkbox?

My Controller

 public async Task<IActionResult> Update(int id)
        {
            var viewModel = await _mediator.Send(new GetRecordById { Id = id });
            return View(viewModel);
        }

UpdateView

 <div class="form-group">
        <label asp-for="IsActive">IsActive</label>
        <input data-val="true"
               data-val-required="The isActive field is required."
               id="IsActive"
               name="IsActive"
               type="checkbox"
               value=true />
        <span asp-validation-for="IsActive" class="text-danger"></span>
    </div>

1 Answers

Just add checked="@viewModel.IsActive" and it will works like charm.

Related