79342204

Date: 2025-01-09 10:18:17
Score: 2
Natty:
Report link

To achieve this in simple way. Append [] to the name attribute. When the form is posted, List will be acptured in the model.

<input name="UserChannelIds[]" value="1" @(Model.UserChannelIds.Contains(1) ? "checked" : "") type="checkbox" />

<input name="UserChannelIds[]" value="2" @(Model.UserChannelIds.Contains(2) ? "checked" : "") type="checkbox" />

During edit, we can bind the "checked" attribute by adding the condition @(Model.UserChannelIds.Contains(2) ? "checked" : "")

Reasons:
  • No code block (0.5):
  • Contains question mark (0.5):
  • Low reputation (1):
Posted by: Patrick_SK