CheckboxList in MVC3

Possible duplicate:
CheckboxList in MVC3 View and retrieve marked items passed to the controller.

How to create a checklist in MVC3 and return the results that were marked when submitted.

Asp.net MVC3

+21
asp.net-mvc-3
Mar 12 2018-11-12T00:
source share
1 answer

There is no helper to do this for you. But it is not so difficult. Assuming you already have a select list in the ViewBag, this will work just fine.

@foreach (var o in ViewBag.Options) { <label><input type="checkbox" name="MyOptions" value="@o.Value"/> <span>@o.Text</span></label> <br/> } 

You see a model that should be able to accept an array, for example ...

 public class MyViewModel { public ICollection<string> MyOptions { get; set; } } 

The selected values ​​will be in MyOptions .

+40
Mar 12 '11 at 20:26
source share



All Articles