MVC实现多选下拉框
借助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>
}
MVC实现多选下拉框的更多相关文章
- MVC实现多选下拉框,保存并显示多选项
在"MVC实现多选下拉框"中,主要是多选下拉框的显示,而实际情况通常是:选择多个选项提交后,需要在编辑页把所有选中的项显示出来. 模拟这样的一个场景:一个车迷可能有多个自己喜欢的汽 ...
- Easyui-Combobox多选下拉框
因为工作需要,引入combobox多选下拉框,并且获取选择的值并以","分开. 效果如下: 代码如下: <html> <head> <title> ...
- Extjs4.2 多选下拉框
//多选下拉框 Ext.define('MDM.view.custom.MultiComboBox', { extend: 'Ext.form.ComboBox', alias: 'widget.mu ...
- js:jquery multiSelect 多选下拉框实例
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- DropDownList单选与多选下拉框
一.单选DropDownList传值 1.添加界面的DropDownList显示值问题 (1)在方法内添加ViewData的方法: var ad = new UnitsRepository(); Vi ...
- pentaho cde 自定义复选下拉框 checkbox select
pentaho 自带的component 虽多,但是当用户需要在一个表格中查看多个组别的数据时,pentaho自带的单选框就不能实现了,所以复选下拉框势在必行,实现效果如下: 实现原理是借用了jqu ...
- Bootstrap3级联多选下拉框
<!DOCTYPE html> <html> <head> <title>Bootstrap3级联多选下拉框</title> <met ...
- js怎么能取得多选下拉框选中的多个值?
方法:获取多选下拉框对象数组→循环判断option选项的selected属性(true为选中,false为未选中)→使用value属性取出选中项的值.实例演示如下: 1.HTML结构 1 2 3 4 ...
- js多选下拉框
1.js原生实现 1.1:引用JS文件 /*! jQuery v1.12.4 | (c) jQuery Foundation | jquery.org/license */ !function(a,b ...
随机推荐
- (二) Log4j 配置详解
第一节: rootLogger 根配置 Log4j 根配置语法 log4j.rootLogger = [ level ] , appenderName, appenderName, … 指代 把指定级 ...
- Effective STL 学习笔记 Item 21:Comparison Function 相关
Effective STL 学习笔记 Item 21:Comparison Function 相关 */--> div.org-src-container { font-size: 85%; f ...
- day6面向对象--继承、多态
继承 继承:就像遗传一样,继承就是拥有父类的所有方法和属性,并且能够定义自己独特的属性和方法,对上面的类进行扩展. 可以什么都不写,直接继承父类,如下: class People(object ...
- CROC 2016 - Elimination Round (Rated Unofficial Edition) E - Intellectual Inquiry dp
E - Intellectual Inquiry 思路:我自己YY了一个算本质不同子序列的方法, 发现和网上都不一样. 我们从每个点出发向其后面第一个a, b, c, d ...连一条边,那么总的不同 ...
- python orm字段解析
null # 是否可以为空 default # 默认值 primary_key # 主键 db_column # 列名 db_index # 索引(db_index=True) unique # 唯一 ...
- CF946D Timetable 动态规划
预处理出每一行去掉$k$个1能获得的最小代价 之后做一次分组背包$dp$即可 预处理可以选择暴力枚举区间... 复杂度$O(n^3)$ #include <set> #include &l ...
- Codeforces.547C.Mike and Foam(容斥/莫比乌斯反演)
题目链接 \(Description\) 给定n个数(\(1\leq a_i\leq 5*10^5\)),每次从这n个数中选一个,如果当前集合中没有就加入集合,有就从集合中删去.每次操作后输出集合中互 ...
- PHP session用redis存储
redis的官方github这么说: phpredis can be used to store PHP sessions. To do this, configure session.save_ha ...
- disable_functions php-fpm root
php.ini disable_functions 禁用某些函数 需要时注意打开 php-fpm 对应conf user group为root时 ERROR: [pool www] please sp ...
- python开发_thread_线程_搜索本地文件
在之前的blog中,曾经写到过关于搜索本地文件的技术文章 如: java开发_快速搜索本地文件_小应用程序 python开发_搜索本地文件信息写入文件 下面说说python中关于线程来搜索本地文件 利 ...