2

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

3

2 Answers 2

12

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()
                      } );
 } 
Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.