0

This is my HTML:

<ul id="limitations" name="limitations" class="dropdown-menu">
   <li>
      <div class="checkbox checkbox-default">
         <input type="checkbox" id="limitation-checkbox-0" value="8" name="limitations[0]" data-name="ALS ">
         <label for="limitation-checkbox-0" style="color:black">ALS </label>
      </div>
   </li>
   <li>
      <div class="checkbox checkbox-primary">
         <input type="checkbox" id="limitation-checkbox-1" value="10" name="limitations[1]" data-name="Arthrogryposis Multiplex Congenita (AMC) &amp; Amyoplasia">
         <label for="limitation-checkbox-1" style="color:black">Arthrogryposis Multiplex Congenita (AMC) &amp; Amyoplasia</label>
      </div>
   </li>
   <li>
      <div class="checkbox checkbox-success">
         <input type="checkbox" id="limitation-checkbox-2" value="2" name="limitations[2]" data-name="Cerebral Palsy (CP), (GMFS)">
         <label for="limitation-checkbox-2" style="color:black">Cerebral Palsy (CP), (GMFS)</label>
      </div>
   </li>
   <li>
      <div class="checkbox checkbox-info">
         <input type="checkbox" id="limitation-checkbox-3" value="4" name="limitations[3]" data-name="Hearing impairment deafness or hard of hearing">
         <label for="limitation-checkbox-3" style="color:black">Hearing impairment deafness or hard of hearing</label>
      </div>
   </li>
    ... MORE <li> ...
</ul>

When I post the form, the model binder for List<int> limitations works only if the checkboxes are selected from, if there is a gap in the array, all items after the gap are not binded.

For example:

Consider the first(8) and the third(10) checkboxes are checked. The value binded to List<int> limitations is [8]. Or if all the checkboxes are checked, List<int> limitations is binded witn an empty list.

I tried changing names of the checkboxes from name="limitations[0..n]" to name="limitations[]" but that caused ASP.NET to completely ignore the values.

Isn't there a standard for such things?!

I know how I can overcome this with javascript manipulation, but I really rather not going that awkward route for such a trivial case.

Using asp.net core 1.0.0 and .Net4.6.1

2
  • It should work if you change the name for all checkboxes to name="limitations". The model binder for int[] limitations or List<int> limitations will work. Commented Jul 20, 2016 at 6:46
  • @tmg, thanks, that did it. Commented Jul 20, 2016 at 7:18

1 Answer 1

1

ASP.Net model binding hasn't changed this behavior between ASP.Net 4 and ASP.Net Core. The answer here is still valid.

Your naming convention is a little off. If you have multiple <input type="checkbox"...> with the same name, it should serialize automatically to the an array that ASP.Net's model binding can accommodate.

Try using name="limitations" instead of name="limitations[0]" throughout your inputs and see if that works as expected.

Sign up to request clarification or add additional context in comments.

2 Comments

BTW, is that the standard? I searched for hours, no one suggested doing it like that. :(
I think the answer is "it depends". I've seen other approaches in other languages/frameworks, but I believe that's how Razor generates your markup - so it's at least standard for ASP.Net.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.