这是我的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) & Amyoplasia">
<label for="limitation-checkbox-1" style="color:black">Arthrogryposis Multiplex Congenita (AMC) & 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>当我发布表单时,List<int> limitations的模型绑定只有在选中复选框时才能工作,如果数组中有空白,则空白后的所有项都不会绑定。
例如:
考虑第一个(8)和第三个(10)复选框被选中。绑定到List<int> limitations的值为8,或者如果选中了所有复选框,则使用空列表绑定List<int> limitations。
我尝试将复选框的name从name="limitations[0..n]"更改为name="limitations[]",但这导致ASP.NET完全忽略了这些值。
难道没有这样的标准吗?!
我知道如何通过javascript操作来克服这个问题,但是对于这样一个琐碎的情况,我真的不愿意走那种尴尬的道路。
使用asp.net核心1.0.0和.Net4.6.1
发布于 2016-07-20 06:56:05
ASP.Net模型绑定并没有改变ASP.Net 4和ASP.Net核心之间的这种行为。The answer here仍然有效。
你的命名惯例有点偏离了。如果您有多个名称相同的<input type="checkbox"...>,它应该自动序列化到ASP.NET的模型绑定可以容纳的数组。
试着在输入中使用name="limitations"而不是name="limitations[0]",看看它是否如预期的那样工作。
https://stackoverflow.com/questions/38473487
复制相似问题