Dynamic list of checkboxes and model binding

前端 未结 2 2104
广开言路
广开言路 2020-12-14 21:13

I\'m trying to create a view that contains a list of checkboxes that is dynamically created from a database, and then retrieve the list of selected ones when the form is pos

相关标签:
2条回答
  • 2020-12-14 21:44

    Have you seen: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx?

    We basically wrote our own control to render the HTML like

    <label for="Products"> Select Products </label>
    <ul class="checkBoxList">
    <li>
        <input type="hidden" value="0" name="Products.Index">
        <input type="checkbox" value="3424" name="Products[0].Id" id="Products0">
        <label for="Products0">iPod touch 3rd Generation</label>
    </li>
    <li>
        <input type="hidden" value="1" name="Products.Index">
        <input type="checkbox" value="3123" name="Products[1].Id" id="Products1">
        <label for="Products1">Creative Zen</label>
    </li>
    </ul>
    </div>
    

    Model Looks Ok, we wrote a custom helper, so our aspx pages look like:

    <%= Html.DropDownFor(m=>m.products) %>
    

    If you follow phil haacks post, your model should automatically bind in your controller.

    0 讨论(0)
  • 2020-12-14 22:09

    Also a good answer in this question: CheckBoxList multiple selections: difficulty in model bind back

    It has a solution that uses a custom Editor Template.

    0 讨论(0)
提交回复
热议问题