How will I populate a ASP.NET MVC List box? Make it non selectable? how will i remove the selected items from the listbox
-
2possible duplicate of MVC ModelBind ListBox With Multple SelectionsGeorge Stocker– George Stocker2010-05-13 12:16:55 +00:00Commented May 13, 2010 at 12:16
-
1Your second question is answered here: stackoverflow.com/questions/1379125/…George Stocker– George Stocker2010-05-13 12:17:33 +00:00Commented May 13, 2010 at 12:17
-
If your list is non selectable how will you select the item to remove?azamsharp– azamsharp2010-05-13 15:00:27 +00:00Commented May 13, 2010 at 15:00
2 Answers
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()
} );
}
Comments
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.