可能重复: CheckboxList在MVC3视图,并得到检查的项目传递给控制器。
如何创建在MVC3一个的CheckBoxList并返回上提交检查结果。
How do you create a checkboxlist in MVC3 and return the results that are checked on submit.
Asp.net MVC3
Asp.net MVC3
有没有帮手内置到为你做这个框架。但是,这并不难。假设你已经有一个选择列表中ViewBag,这会工作得很好。
There's no helper built into the framework to do this for you. But it's not that difficult. Assuming that you already have a select list in 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 view model will need to be able to accept an array, like this...
public class MyViewModel {
public ICollection<string> MyOptions { get; set; }
}
选择将在 MyOptions
的值。