vendredi 22 novembre 2019

Serializing a form to capture checked checkboxes

I have a form with a number of checkboxes. I want to be able to serialize this form and create a UserClaim list of the checkboxes. Ideally, I will like to iterate over the checkboxes and create a UserClaim object for each claim, and add this object to a UserClaim list. I need this list to pass as Json data to a controller method. None of the examples in the forum could help me so far.

public class UserClaim
    {
        public string ClaimValue { get; set; }
        public bool IsSelected { get; set; }
    }
<form id="manageClaimsForm" asp-action="ManageUserClaims" asp-controller="ApplicationUser" method="post">
<div class="modal-header">
  <h5><b>Manage User Claims</b></h5>
</div>
<input type="hidden" asp-for="@Model.UserId" />   
<div>
  @for (int i = 0; i < Model.Claims.Count; i++)
  {
    <div class="form-check m-1">
      <input type="hidden" asp-for="@Model.Claims[i].ClaimValue" />
      <input asp-for="@Model.Claims[i].IsSelected" class="form-check-input" />
      <label class="form-check-label" asp-for="@Model.Claims[i].IsSelected">
        @Model.Claims[i].ClaimValue
      </label>
    </div>
  }
<div class="modal-footer">
  <button id="claimsManage-close-button" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  <input type="button" value="Update" class="btn btn-primary" onclick="submitNewClaimsSet()"/>
</div>
</div>



Aucun commentaire:

Enregistrer un commentaire