I am using ASP.NET MVC to populate a group of checkboxes. These checkboxes change depending on what the user selects on the previous view. I am using Razor. In the view I have:
@foreach (var skills in Model.SkillsAvailable)
{
<input name="pack" type="checkbox" value="@skills" /> @skills<br />
}
@Html.HiddenFor(m => m.SkillsSelected, new { @class = "hidden" })
I have a few others that look nearly identical to this but are single selection radio buttons. It functions by jQuery taking the selected option on change and populating the HiddenFor. The HiddenFor is passed to the control upon submit. No issues with that.
But for my checkbox, I'm not sure how to send multiple items back. If this list populates 10 items, and the user can only select 2, how do I add them to List<string> SkillsSelected?
My first instinct is that I can add somehow using another foreach loop (like foreach that is selected, add to a list). But I have no idea what that looks like, and Google searching isn't bringing anything up except for using bools, which makes sense if the list is the same every time, but not if the list has between 5 and 30 options depending on the previous selection.
At this point in time, I don't have any jQuery controlling this section, so I don't have code for it. My controller is populating the list of checkboxes just fine, but not handling anything else.
The only properties in my model that matters are these, and I have no problem changing them in necessary.:
public List<string> SkillsAvailable { get; set; }
public List<string> SkillsSelected { get; set; }
Aucun commentaire:
Enregistrer un commentaire