ListBoxFor不让我选择多个项目MVC [英] ListBoxFor not letting me select multiple items MVC

查看:332
本文介绍了ListBoxFor不让我选择多个项目MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行代码时,我一次只能选择一个项目,这很奇怪,因为'ListBoxFor()'用于选择多个项目,所以我想要的是:



选择多个项目

查看(Index.cshtml):

 < DIV> 
@ Html.ListBoxFor(m => m.DropDownItems,new MultiSelectList(Repository.DDFetchItems(),Value,Text,Model.DropDownItems))
< / div>

模型(ModelVariables.cs):

  public class ModelVariables 
{
public List< SelectListItem> DropDownItems {get;组; }
}

public static class Repository
{
public static List< SelectListItem> DDFetchItems()
{
return new List< SelectListItem>()
{
New SelectListItem(){Text =Dogs,Value =1,Selected = true} ,
new SelectListItem(){Text =Cats,Value =2},
SelectListItem(){Text =Death,Value =3}
};




$ p (HomeController.cs):



pre $ $ $ $ $ $ $ $ $ $ $ $ $ {
$ {
$ $ b {
DropDownItems = Repository.DDFetchItems()
};
return View(model);


解决方案

c $ c><多个> 到一组复杂对象(这就是 List< SelectListItem> 是)。 < select multiple> 回传一组简单值(在你的情况下,如果你选择第一个和第三个选项,它会提交您的模型需要一个 IEnumerable< int> (所选选项的值)。 / code>要绑定的属性。

  public class ModelVariables 
{
public IEnumerable< int> SelectedItems {get; set;}
public IEnumerable< SelectListItem> DropDownItems {get; set;}
}

然后在GET方法中

  public ActionResult Index()
{
var ModelVariables = new ModelVariables()
{
DropDownItems = Repository.DDFetchItems(),
SelectedItems = new List< int>(){1,3} //预先选择第一和第三选项
};
返回视图(模型);
}

和在视图中

  @ Html.ListBoxFor(m => m.SelectedItems,Model.DropDownItems)

附注


  1. DDFetchItems()方法中删除 Selected = true - 它的
    ListBoxFor()方法忽略,因为它的
    属性的值与绑定的值决定了选择的内容。
  2. 不需要从
    第一个 ListBoxFor() SelectList c>方法(property DropDownItems
    已经是 IEumerable< SelectListItem>


When I run the code, I can only select one item at a time, that's weird because 'ListBoxFor()' is used to select multiple items, so what i want is:

Select multiple items

View (Index.cshtml):

<div>
    @Html.ListBoxFor(m => m.DropDownItems, new MultiSelectList(Repository.DDFetchItems(), "Value", "Text", Model.DropDownItems))
</div>

Model (ModelVariables.cs):

public class ModelVariables
{
    public List<SelectListItem> DropDownItems { get; set; } 
}

public static class Repository
{
    public static List<SelectListItem> DDFetchItems()
    {
        return new List<SelectListItem>()
        {
            new SelectListItem(){  Text = "Dogs", Value = "1", Selected = true},
            new SelectListItem(){  Text = "Cats", Value = "2"},
            new SelectListItem(){  Text = "Death", Value = "3"}
        };
    }
}

Controller (HomeController.cs):

[HttpGet]
public ActionResult Index()
{
    ModelVariables model = new ModelVariables()
    {
        DropDownItems = Repository.DDFetchItems()  
    };
    return View(model);
}

解决方案

You cannot bind a <select multiple> to a collection of complex objects (which is what List<SelectListItem> is). A <select multiple> posts back an array of simple values (in your case, if you select the 1st and 3rd options, it will submit [1, 3] (the values of the selected options).

Your model needs a IEnumerable<int> property to bind to.

public class ModelVariables
{
    public IEnumerable<int> SelectedItems { get; set; }
    public IEnumerable<SelectListItem> DropDownItems { get; set; }
}

and then in the GET method

public ActionResult Index()
{
    var ModelVariables= new ModelVariables()
    {
        DropDownItems = Repository.DDFetchItems(),
        SelectedItems = new List<int>(){ 1, 3 } // to preselect the 1st and 3rd options
    };
    return View(model);
}

and in the view

@Html.ListBoxFor(m => m.SelectedItems, Model.DropDownItems)

Side notes

  1. Remove Selected = true in the DDFetchItems() method - its ignored by the ListBoxFor() method because its the value of the property your binding to which determines what is selected
  2. There is not need to build a new identical SelectList from the first one inside the ListBoxFor() method (property DropDownItems is already IEumerable<SelectListItem>)

这篇关于ListBoxFor不让我选择多个项目MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆