How will I populate a ASP.NET MVC List box? Make it non selectable? how will i remove the selected items from the l开发者_如何学运维istbox
You want an unselectable select?
<%= Html.ListBoxFor( m => m.Choices,
Model.ChoicesMenu,
new { disabled = "disabled" } ) %>
The idea is that your model needs to have an IEnumerable<SelectListItem>
that will hold the possible key/value pairs for your selection, here the ChoicesMenu
. The actual values chosen, if it could be selected, would be posted in the Choices
property. Use the signature that allows you to specify html attributes and make it disabled prevent selecting it. You can, of course, do this (or undo it) with javascript.
Model:
public class ViewModel
{
public int[] Choices { get; set; }
public IEnumerable<SelectListItem> ChoicesMenu { get; set; }
}
Action (relevant bit)
var model = new ViewModel
{
ChoicesMenu = db.Items
.Select( i => new SelectListItem
{
Text = i.Name,
Value = i.ID.ToString()
} );
}
MVC ModelBind ListBox With Multple Selections Might give you the first answer.
You can disable the items in the listbox, but not the list box itself. If you set Visible to false, the whole listbox will not display.
Doing something like:
ListBox.Items[X].Selected = false
Will make the items non selectable.
精彩评论