2017-08-30 26 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" }); 

它適用於我。非常感謝你。