简体   繁体   English

asp.net html.listboxfor

[英]asp.net html.listboxfor

How will I populate a ASP.NET MVC List box? 我将如何填充ASP.NET MVC列表框? 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 . 我们的想法是你的模型需要有一个IEnumerable<SelectListItem> ,它将为你的选择保留可能的键/值对,这里是ChoicesMenu The actual values chosen, if it could be selected, would be posted in the Choices property. 选择的实际值(如果可以选择)将在Choices属性中发布。 Use the signature that allows you to specify html attributes and make it disabled prevent selecting it. 使用允许您指定html属性的签名并使其禁用以防止选择它。 You can, of course, do this (or undo it) with javascript. 当然,您可以使用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. 具有多种选择的MVC ModelBind ListBox可能会为您提供第一个答案。

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. 如果将Visible设置为false,则不会显示整个列表框。

Doing something like: 做类似的事情:

ListBox.Items[X].Selected = false

Will make the items non selectable. 将使项目不可选。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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