开发者

asp.net html.listboxfor

开发者 https://www.devze.com 2022-12-29 23:35 出处:网络
How will I populate a ASP.NET MVC List box? Make it non selectable? how will i remove the selected items from the l开发者_如何学运维istboxYou want an unselectable select?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号