开发者

CheckboxList in MVC3 [duplicate]

开发者 https://www.devze.com 2023-02-16 22:39 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: CheckboxList in MVC3 View and get the checked items passed to the controller.
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

CheckboxList in MVC3 View and get the checked items passed to the controller.

How do you create a checkboxl开发者_Python百科ist in MVC3 and return the results that are checked on submit.

Asp.net MVC3


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; }
}

The values selected will be in MyOptions.

0

精彩评论

暂无评论...
验证码 换一张
取 消