简体   繁体   中英

asp.net html.listboxfor

How will I populate a ASP.NET MVC List box? Make it non selectable? how will i remove the selected items from the listbox

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM