Hi
Change the checked status of multi-select checkbox in asp.net core mvc
I am saving a multi-select checkbox as a comma delimited value in the database.
For the edit action, I am trying to split the comma delimited string into a string array and to check the selections accordingly.
I think I am missing something!
Please help
@model CheckboxList_Net_Core5.Models.Registration
@{
ViewData["Title"] = "Edit";
string[] OrgListArray = Model.SelectedOrgs.Split(", ");
}
<h1>Edit</h1>
<h4>Registration</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Edit">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="CustomerCode" class="control-label"></label>
<input asp-for="CustomerCode" class="form-control" />
<span asp-validation-for="CustomerCode" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="CustomerName" class="control-label"></label>
<input asp-for="CustomerName" class="form-control" />
<span asp-validation-for="CustomerName" class="text-danger"></span>
</div>
<div class="form-group">
@*<label asp-for="SelectedOrgs" class="control-label"></label>
<input asp-for="SelectedOrgs" class="form-control" />
<span asp-validation-for="SelectedOrgs" class="text-danger"></span>*@
<table>
@foreach (SelectListItem Org in Model.OrgList)
{
<tr>
<td>
<input id="@Org.Value" type="checkbox" name="organizations" value="@Org.Value" checked="@if (OrgListArray.Contains(Org.Value)){ "checked" }" />
</td>
<td>
<label for="@Org.Value">@Org.Text</label>
</td>
</tr>
}
</table>
</div>
<div class="form-group">
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
I am getting ";" needed for the line
<td>
<input id="@Org.Value" type="checkbox" name="organizations" value="@Org.Value" checked="@if (OrgListArray.Contains(Org.Value)){ "checked" }" />
</td>
Kindly help.
Rajesh