ASP.NET?MVC實(shí)現(xiàn)多選下拉框
借助Chosen Plugin可以實(shí)現(xiàn)多選下拉框。
選擇多項(xiàng):
設(shè)置選項(xiàng)數(shù)量,比如設(shè)置最多允許2個(gè)選項(xiàng):
Model模塊
考慮到多選下拉<select multiple="multiple"...></select>選中項(xiàng)是string數(shù)組,Model應(yīng)該這樣設(shè)計(jì):
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 = "斯柯達(dá)" }); 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="external nofollow" rel="stylesheet" /> @using (Html.BeginForm("SaveCars", "Home", FormMethod.Post)) { @Html.LabelFor(m => m.SelectedCars,"最多選擇2個(gè)選項(xiàng)") <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> }
到此這篇關(guān)于ASP.NET MVC實(shí)現(xiàn)多選下拉框的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解ASP.NET MVC 下拉框的傳值的兩種方式
- ASP .NET 可編輯輸入自動匹配的下拉框
- 詳解ASP.NET MVC之下拉框綁定四種方式
- ASP.NET MVC下拉框聯(lián)動實(shí)例解析
- ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
- ASP.NET實(shí)現(xiàn)級聯(lián)下拉框效果實(shí)例講解
- ASP.NET多彩下拉框開發(fā)實(shí)例
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- asp.net中js+jquery添加下拉框值和后臺獲取示例
- asp.net 實(shí)現(xiàn)下拉框只讀功能
- ASP.NET?MVC下拉框中顯示枚舉項(xiàng)
相關(guān)文章
ABP引入SqlSugar框架的簡單版創(chuàng)建使用
這篇文章主要為大家介紹了ABP引入SqlSugar框架的簡單版創(chuàng)建使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04如何利用FluentMigrator實(shí)現(xiàn)數(shù)據(jù)庫遷移
這篇文章主要給大家介紹了關(guān)于如何利用FluentMigrator實(shí)現(xiàn)數(shù)據(jù)庫遷移的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Entity?Framework生成DataBase?First模式
本文詳細(xì)講解了Entity?Framework生成DataBase?First模式的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03ASP.NET實(shí)現(xiàn)個(gè)人信息注冊頁面并跳轉(zhuǎn)顯示
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)個(gè)人信息注冊頁面并跳轉(zhuǎn)顯示的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12創(chuàng)建基于ASP.NET的SMTP郵件服務(wù)的具體方法
Asp.net在System.Web.Mail名稱空間中有一個(gè)發(fā)送email的內(nèi)建類,但這僅是cdosys的一個(gè)假象。開發(fā)者能使用一個(gè)替代的它smtp郵件服務(wù)。在這篇文章里面,我將會展示如何創(chuàng)建一個(gè)用于asp.net的功能齊全的smtp郵件服務(wù)2013-11-11ASP.NET?Core?MVC自定義Tag?Helpers用法介紹
這篇文章介紹了ASP.NET?Core?MVC自定義Tag?Helpers的用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02ASP.NET Core根據(jù)環(huán)境變量支持多個(gè) appsettings.json配置文件
這篇文章主要介紹了ASP.NET Core根據(jù)環(huán)境變量支持多個(gè) appsettings.json配置文件,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Visual Studio Debug實(shí)戰(zhàn)教程之?dāng)帱c(diǎn)操作
眾所周知斷點(diǎn)對于Visual Studio調(diào)試過程是十分重要的,斷點(diǎn)的設(shè)置也是為了更好的進(jìn)行調(diào)試。下面這篇文章主要給大家介紹了關(guān)于Visual Studio Debug實(shí)戰(zhàn)教程之?dāng)帱c(diǎn)操作的相關(guān)資料,需要的朋友可以參考下2018-09-09ASP.NET Core WebAPI實(shí)現(xiàn)本地化(單資源文件)
這篇文章主要介紹了ASP.NET Core WebAPI實(shí)現(xiàn)本地化(單資源文件),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06