我正在使用jquery在下拉更改事件中动态添加行。行包含复选框,TextArea和Dropdown。所有控件都与模型属性绑定。复选框也与Model的bool属性绑定。在提交表单时,控制器会收到模型,其他控件(如TextArea和dropdown)都在表单中输入了值,但尽管选中了复选框,但checkbox属性始终为false。
这是HtmlHelper类用于绑定模型的方式。
<tr>
<td>
@Html.CheckBoxFor(model => model.FunctionalRequirementsList[i].isSelected, new { @class = "checkbox" })
@Html.HiddenFor(model => model.FunctionalRequirementsList[i].FunctionalRequirementId)
</td>
<td>
@Html.TextAreaFor(model => model.FunctionalRequirementsList[i].FunctionalRequirement, 5, 80, new { @class = "text form-control col-md-10", style = "background-color:#fcf999;max-width:100%;height:50px;", ReadOnly = "true" })
</td>
<td>
@Html.DropDownListFor(model => model.FunctionalRequirementsList[i].DefaultPriorityId, new SelectList(Model.FunctionalRequirementsList[i].ApllicabilityOptions, "Value", "Text", Model.FunctionalRequirementsList[i].DefaultPriorityId), new { @class = "form-control col-md-1" })
</td>
</tr>
用于动态添加的jQuery代码:
var oldFRSelected_Id = $trlast.find("input[type='checkbox'][id*='isSelected']").attr('id')
var oldFRSelected_Id_Slice = oldFRSelected_Id.split('_');
var newFRSelected_Id = oldFRSelected_Id_Slice[0] + "_" + (i + 1) + "__" + oldFRSelected_Id_Slice[3]
var newFRSelected_name = oldFRSelected_Id_Slice[0] + "[" + (i + 1) + "]." + oldFRSelected_Id_Slice[3]
$trclone.find("input[type='checkbox'][id*='isSelected']").attr('id', newFRSelected_Id);
$trclone.find("input[type='checkbox'][id*='isSelected']").attr('name', newFRSelected_name);