Asp.net html.listboxfor

How can I populate an ASP.NET MVC List? Make it inaccessible? how do i remove selected items from list

+2
source share
2 answers

Do you want to choose a non-selective choice?

 <%= Html.ListBoxFor( m => m.Choices, 
                      Model.ChoicesMenu,
                      new { disabled = "disabled" } ) %>

The idea is that your model should have IEnumerable<SelectListItem>one that will contain possible key / value pairs for your selection, here ChoicesMenu. The actual values ​​selected, if they can be selected, will be published in the property Choices. Use a signature that allows you to specify html attributes and disable it so you don’t select it. You can of course do this (or undo) using javascript.

Model:

 public class ViewModel
 {
     public int[] Choices { get; set; }
     public IEnumerable<SelectListItem> ChoicesMenu { get; set; }
 }

Action (corresponding bit)

 var model = new ViewModel
 {
     ChoicesMenu = db.Items
                     .Select( i => new SelectListItem
                      {
                          Text = i.Name,
                          Value = i.ID.ToString()
                      } );
 } 
+11
source

MVC ModelBind ListBox .

, . Visible false, .

- :

ListBox.Items[X].Selected = false

.

+1

Source: https://habr.com/ru/post/1794257/


All Articles