简体   繁体   中英

MVC multiselect selected values without model

I have got a simple form and it has got a MultiSelectList inside. This form method is GET. When I choose more items in SelectList and submit form, I want to see what I choose. I use SelectedValues for that. But it selects only first item in MultiSelectList .

My View :

<form method="get">
    @Html.DropDownList("kategori", new MultiSelectList(ViewBag.KategoriSelect, "id", "isim", ViewBag.KategoriSelected),
 new
 {
     @multiple = "true ",
     @size = "8"
 })

    <input type="submit" value="gönder" />
</form>

My Controller :

    public ActionResult Index(int[] kategori)
    {
        var db = new MyDbContext();

        var aracs = db.aracs.ToList();

        //items for multi select
        ViewBag.KategoriSelect = db.kategoris.ToList();

        //selected items
        ViewBag.KategoriSelected = kategori;

        return View(aracs);
    }

My Get URL : http://localhost:6151/?kategori=2&kategori=1&kategori=4&kategori=3

HTML output :
HTML输出,用于多选

But it must select all item. what must I do for that ?

I have worked on similar scenario where we use https://github.com/davidstutz/bootstrap-multiselect

Bootstrap Multiselect which can be loaded as list fro C# and converts to Multiselect with built in JS library.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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