asp.net mvc - How can I limit the maximum number of selected CheckBoxes? -


on page there checkboxlist. create this:

@foreach (var project in model.projects) {     <input type="checkbox"          id="@project.name"         name="projects"         value="@project.id"         title="@project.name" />     <label for="@project.name">@project.name</label><br/> } 

how can limit maximum number of selected checkboxes?

try this:

 int count = 1;  int maxnoofselected = 1;  @foreach (var project in model.projects) {     if(count <= maxnoofselected)     {         <input type="checkbox"          id="@project.name"         name="projects"         value="@project.id"         title="@project.name"         checked="checked" />         <label for="@project.name">@project.name</label><br/>         @count++;     }     else     {             <input type="checkbox"          id="@project.name"         name="projects"         value="@project.id"         title="@project.name" />         <label for="@project.name">@project.name</label><br/>     } } 

if count less or equal maxnoofselected (i.e. number) check box checked otherwise unchecked.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -