我正在一个网站上工作,在这种情况下,一些输入数据应该是可见的,但对于用户输入是禁用的。由于禁用输入的数据不会发送回服务器,因此我总是添加一个隐藏输入以发送回服务器。这样数据就不会被空值覆盖。
现在我在渲染禁用的 ListBox 时遇到了问题。目前,我呈现该 ListBox 的副本,为用户隐藏。有没有更好的方法来获得相同的结果?
代码说明:
@*Works fine*@
@if (Model.OnEdit)
{
@Html.HiddenFor(x => x.Contract)
@Html.DropDownListFor(x => x.Contract,
Model.ContractList,
new { disabled = "disabled" })
}
@if (Model.OnEdit)
{
@*Otherwise the items will be updated to null*@
@Html.ListBoxFor(x => x.Items,
Model.ItemList,
new { @class = "hidden" })
@Html.ListBoxFor(x => x.Items,
Model.ItemList,
new { disabled = "disabled" })
}