mercredi 23 octobre 2019

How to get checkbox selected in ASP.NET MVC from database?

I have this model

public class UserModel
{
   public List<UserModel> Skills { get; set; }
   public int SkillId { get; set; }
   public string SkillName { get; set; }
   public bool IsSelected{ get; set; }
}

In Edit.cshtml

@for (var i = 0; i < Model.Skills.Count; i++)
{
            <div>
                @Html.HiddenFor(x => x.Skills[i].SkillId)
                @Html.CheckBoxFor(x => x.Skills[i].IsSelected, new { htmlAttributes = new { @class = "form-control } })
                @Html.DisplayFor(x => x.Skills[i].SkillName, new { htmlAttributes = new { @class = "form-control" } })
            </div>
}

In database I have saved SkillId. On the basis of skillId, how to get checkbox selected when editing?




Aucun commentaire:

Enregistrer un commentaire