2017-08-30 72 views
0

我有以下ListBoxFor在HTML页面,我几乎尝试了一切,使之只读,但仍然没有运气:如何在c#MVC中使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), 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" } })) 

任何建议都会有所帮助。

谢谢。

+0

listbox本身只读,你试着禁用它,对吧?怎么看这个? https://forums.asp.net/t/1840939.aspx?How+to+make+ListBox+Read+only – Se0ng11

+0

'edit'属于什么?我认为'new {@readonly =“readonly”}'或'new {@disabled =“disabled”}'就足够了(NB:'disable'属性阻止提交列表框的值)。 –

回答

1

HTML中的<select>元素没有readonly属性,因为它不需要一个:用户不能直接编辑0​​元素的内容:它们只能更改其选择。

如果你想使用户不能更改选择的,然后使用<ul><ol>呈现一个普通的HTML列表,有一个自定义样式时限制高度,并允许滚动列表框:

剃刀:

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

CSS:

ul.read-only-listbox { 
    height: 500px; 
    list-style: none; 
    overflow: auto; 
    border: 1px inset #ccc; 
    background: white; 
} 
ul.read-only-listbox li { 
    display: block; 
} 
0

它的工作对我来说

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

它适用于我。非常感谢你。