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)多選下拉框的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 詳解ASP.NET MVC 下拉框的傳值的兩種方式
- ASP .NET 可編輯輸入自動(dòng)匹配的下拉框
- 詳解ASP.NET MVC之下拉框綁定四種方式
- ASP.NET MVC下拉框聯(lián)動(dòng)實(shí)例解析
- ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
- ASP.NET實(shí)現(xiàn)級(jí)聯(lián)下拉框效果實(shí)例講解
- ASP.NET多彩下拉框開發(fā)實(shí)例
- asp.net mvc下拉框Html.DropDownList 和DropDownListFor的常用方法
- asp.net中js+jquery添加下拉框值和后臺(tái)獲取示例
- asp.net 實(shí)現(xiàn)下拉框只讀功能
- ASP.NET?MVC下拉框中顯示枚舉項(xiàng)
相關(guān)文章
ASP.NET中GridView 重復(fù)表格列合并的實(shí)現(xiàn)方法
本文通過GridView 和 Repeater 解決有關(guān)表格顯示數(shù)據(jù)重復(fù)的數(shù)據(jù)列和并的方法,非常實(shí)用,感興趣的朋友一起看下吧2016-08-08
.net頁(yè)面訪問次數(shù)統(tǒng)計(jì)實(shí)現(xiàn)原理與代碼
網(wǎng)站訪問量統(tǒng)計(jì)、頁(yè)面訪問次數(shù)統(tǒng)計(jì),比較實(shí)用的一個(gè)功能,很多新手朋友都想實(shí)現(xiàn),本文處于此目的整理了一些,感興趣的朋友可以了解下2013-01-01
詳解ASP.NET數(shù)據(jù)綁定操作中Repeater控件的用法
.NET中的Repeater控件支持?jǐn)?shù)據(jù)模板,而且可以自由地定義樣式,這里我們就來詳解ASP.NET數(shù)據(jù)綁定操作中Repeater控件的用法,需要的朋友可以參考下2016-06-06

