MVC實現多選下拉框

來源:互聯網
上載者:User

標籤:des   c   style   class   blog   code   

藉助Chosen Plugin可以實現多選下拉框。

 

選擇多項:

 

設定選項數量,比如設定最多允許2個選項:

 

考慮到多選下拉<select multiple="multiple"...></select>選中項是string數組,Model應該這樣設計:

using System.Collections.Generic;using System.Web.Mvc;namespace MvcApplication1.Models{    public class CarVm    {         public string[] SelectedCars { get; set; }        public IEnumerable<SelectListItem>  AllCars { get; set; }    }}

 

HomeController中:

using System.Collections.Generic;using System.Linq;using System.Web.Mvc;using MvcApplication1.Models;namespace MvcApplication1.Controllers{    public class HomeController : Controller    {        public ActionResult Index()        {            CarVm carVm = new CarVm();            carVm.SelectedCars = new string[]{"1","2"};            carVm.AllCars = GetAllCars();            return View(carVm);        }        [HttpPost]        public ActionResult SaveCars(CarVm carVm)        {            if (ModelState.IsValid)            {                return View(carVm);            }            return RedirectToAction("Index", carVm);        }        private IEnumerable<SelectListItem> GetAllCars()        {            List<SelectListItem> allCars = new List<SelectListItem>();            allCars.Add(new SelectListItem() {Value = "1",Text = "平治"});            allCars.Add(new SelectListItem() { Value = "2", Text = "寶馬" });            allCars.Add(new SelectListItem() { Value = "3", Text = "奇瑞" });            allCars.Add(new SelectListItem() { Value = "4", Text = "比亞迪" });            allCars.Add(new SelectListItem() { Value = "5", Text = "起亞" });            allCars.Add(new SelectListItem() { Value = "6", Text = "福士" });            allCars.Add(new SelectListItem() { Value = "7", Text = "斯柯達" });            allCars.Add(new SelectListItem() { Value = "8", Text = "豐田" });            allCars.Add(new SelectListItem() { Value = "9", Text = "本田" });            return allCars.AsEnumerable();        }    }}


Home/Index.cshtml視圖中,需要引用Chosen Plugin的css和js檔案:

@model MvcApplication1.Models.CarVm@{    ViewBag.Title = "Index";    Layout = "~/Views/Shared/_Layout.cshtml";}<h2>Index</h2><link href="~/Content/chosen.css" rel="stylesheet" />@using (Html.BeginForm("SaveCars", "Home", FormMethod.Post)){    @Html.LabelFor(m => m.SelectedCars,"最多選擇2個選項") <br/>    @Html.ListBoxFor(m => m.SelectedCars, Model.AllCars, new {@class="chosen", multiple="multiple", style="width:350px;"}) <br/>    <input type="submit" value="提交"/>}@section scripts{    <script src="~/Scripts/chosen.jquery.min.js"></script>    <script type="text/javascript">        $(function() {            $(‘.chosen‘).chosen({                max_selected_options: 2            });            $(".chosen-deselect").chosen(            {                allow_single_deselect: true             });            $(".chosen").chosen().change();            $(".chosen").trigger(‘liszt:updated‘);        });    </script>}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.