我想知道是否有人知道为什么我的dropdownlistfor没有改变,并在我更改选择列表中的选项时将新值发回控制器。选择列表位于forloop中,显示多个具有多个选择列表的项目,因此用户可以将他们选择的项目更改为新项目。
@for (int i = 0; i < Model.PoItemsList.Count; i++)
{
net += Convert.ToDecimal(Model.PoItemsList[i].Net);
vat += Convert.ToDecimal(Model.PoItemsList[i].Vat);
gross += Convert.ToDecimal(Model.PoItemsList[i].Gross);
<tr>
<td>
@Html.HiddenFor(m => m.PoItemsList[i].Id)
@Html.HiddenFor(m => m.PoItemsList[i].NCodeId)
@Html.TextBoxFor(m => m.PoItemsList[i].ItemRef, new { @class = "form-control" })
</td>
<td>
@Html.TextBoxFor(m => m.PoItemsList[i].Description, new { @class = "form-control", @style = "width: 200px;" })
</td>
<td>
@*@Html.DropDownListFor(m => m.PoItemsList[i].NCodeId, Model.NominalCodeList, new { @class = "form-control", @id = "ncode" })*@
@Html.DropDownListFor(m => m.PoItemsList[i].NCodeId, new SelectList(Model.NominalCodeList, "Value", "Text", Model.PoItemsList[i].NCodeId), new { @class = "form-control"})
</td>
<td>
@Html.TextBoxFor(m => m.PoItemsList[i].CostPerItem, new { @class = "form-control", @Value = Convert.ToDecimal(Model.PoItemsList[i].CostPerItem) })
</td>
<td>
@Html.TextBoxFor(m => m.PoItemsList[i].Quantity, new { @class = "form-control", @style = "width: 50px;" })
</td>
<td>
@Html.TextBoxFor(m => m.PoItemsList[i].Net, new { @class = "form-control cpd-align-right", @Value = Convert.ToDecimal(Model.PoItemsList[i].Net) })
</td>
<td>
@Html.TextBoxFor(m => m.PoItemsList[i].Vat, new { @class = "form-control cpd-align-right", @Value = Convert.ToDecimal(Model.PoItemsList[i].Vat) })
</td>
<td>
@Html.TextBoxFor(m => m.PoItemsList[i].Gross, new { @class = "form-control cpd-align-right", @Value = Convert.ToDecimal(Model.PoItemsList[i].Gross) })
</td>
<td>
@Html.CheckBoxFor(m => m.ManualEntry, new {@class = "form-control"})
</td>
<td>
<input type="button" class="btn btn-danger centre" value="X" onclick="DeleteItemAndSave(@Model.PoItemsList[i].Id)" />
</td>
</tr>
}
答案 0 :(得分:1)
您已将@Html.HiddenFor(m => m.PoItemsList[i].NCodeId)
和下拉列表绑定到同一项目;删除隐藏的输入,您的代码应该可以正常工作。