日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

ASP.NET?MVC實現多選下拉框_實用技巧

作者:Darren?Ji ? 更新時間: 2022-09-24 編程語言

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

選擇多項:

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

Model模塊

考慮到多選下拉<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"    />

@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>
}

原文鏈接:https://www.cnblogs.com/darrenji/p/3763681.html

欄目分類
最近更新