如何在c#MVC中使ListBoxFor只读?

如何在c#MVC中使ListBoxFor只读?,c#,html,asp.net-mvc,C#,Html,Asp.net Mvc,我在HTML页面中有下面的ListBoxFor,我尝试了几乎所有的方法使其只读,但仍然没有成功: @Html.ListBoxFor(m => m.SelectedLocations, Model.GetLocationsNotInBuildingList(Model.idCompany, null), (edit ? extraData : new Dictionary<string, object> { { "disabled", "" } })) @Html.ListBo

我在HTML页面中有下面的ListBoxFor,我尝试了几乎所有的方法使其只读,但仍然没有成功:

@Html.ListBoxFor(m => m.SelectedLocations, Model.GetLocationsNotInBuildingList(Model.idCompany, null), (edit ? extraData : new Dictionary<string, object> { { "disabled", "" } }))
@Html.ListBoxFor(m=>m.SelectedLocations,Model.GetLocationsNotInBuildingList(Model.idCompany,null),(编辑?外部数据:新字典{{“已禁用”,“已禁用”)
我也尝试过以下解决方案,但均无效:

@Html.ListBoxFor(m => m.SelectedLocations, Model.GetLocationsNotInBuildingList(Model.idCompany, null), new { @readonly = "readonly" })

@Html.ListBoxFor(m => m.SelectedLocations, Model.GetLocationsNotInBuildingList(Model.idCompany, null), (edit ? extraData : new Dictionary<string, object> { { "readonly", "readonly" } }))

@Html.ListBoxFor(m => m.SelectedLocations, Model.GetLocationsNotInBuildingList(Model.idCompany, null), (edit ? extraData : new Dictionary<string, object> { { "disabled", "disabled" } }))
@Html.ListBoxFor(m=>m.SelectedLocations,Model.GetLocationsNotInBuildingList(Model.idCompany,null),new{@readonly=“readonly”})
@Html.ListBoxFor(m=>m.SelectedLocations,Model.GetLocationsNotInBuildingList(Model.idCompany,null),(编辑?extraData:newdictionary{{{“readonly”,“readonly”}))
@Html.ListBoxFor(m=>m.SelectedLocations,Model.GetLocationsNotInBuildingList(Model.idCompany,null),(编辑?外部数据:新字典{{{“禁用”,“禁用”}))
任何建议都会有帮助


谢谢。

HTML中的
元素没有
只读属性,因为它不需要属性:用户无法直接编辑
元素的内容:他们只能更改其选择

如果您想要一个用户无法更改选择的列表框,请使用
呈现普通HTML列表,并使用自定义样式限制高度并允许滚动:

剃刀:

<ul class="read-only-listbox">
@foreach(ListItem item in listItems) {
    <li>@item.Text</li>
}
</ul>
它对我有用

@Html.ListBoxFor(m => m.SelectedCities, Model.Cities, new {@disabled="disabled" });

它对我有用。非常感谢。

列表框本身已经是只读的,您尝试禁用它,对吗?看看这个怎么样?
edit
属于什么?我认为
new{@readonly=“readonly”}
new{@disabled=“disabled”}
就足够了(注意:
disable
属性阻止提交列表框值)。
@Html.ListBoxFor(m => m.SelectedCities, Model.Cities, new {@disabled="disabled" });
@Html.ListBoxFor(m => m.SelectedCities, Model.Cities, new {@disabled="disabled" });