最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET?MVC實(shí)現(xiàn)多選下拉框

 更新時(shí)間:2022年07月31日 10:35:51   作者:Darren?Ji  
這篇文章介紹了ASP.NET?MVC實(shí)現(xiàn)多選下拉框的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

借助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í)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

黎平县| 舞阳县| 侯马市| 西充县| 余庆县| 饶河县| 青铜峡市| 孟连| 秭归县| 固安县| 怀安县| 思南县| 柳州市| 玉屏| 延津县| 吉隆县| 马尔康县| 鄂托克前旗| 霍州市| 博湖县| 博兴县| 泗洪县| 颍上县| 安达市| 杭锦后旗| 平遥县| 乐都县| 蒲城县| 郎溪县| 高密市| 安远县| 泊头市| 平舆县| 绥滨县| 广汉市| 宜兰市| 鸡西市| 通山县| 七台河市| 陵川县| 德保县|