亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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)文章

  • ABP引入SqlSugar框架的簡單版創(chuàng)建使用

    ABP引入SqlSugar框架的簡單版創(chuàng)建使用

    這篇文章主要為大家介紹了ABP引入SqlSugar框架的簡單版創(chuàng)建使用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-04-04
  • 如何利用FluentMigrator實(shí)現(xiàn)數(shù)據(jù)庫遷移

    如何利用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-04
  • Entity?Framework生成DataBase?First模式

    Entity?Framework生成DataBase?First模式

    本文詳細(xì)講解了Entity?Framework生成DataBase?First模式的方法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-03-03
  • ASP.NET實(shí)現(xiàn)個(gè)人信息注冊頁面并跳轉(zhuǎn)顯示

    ASP.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ù)的具體方法

    創(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-11
  • ASP.NET?Core?MVC自定義Tag?Helpers用法介紹

    ASP.NET?Core?MVC自定義Tag?Helpers用法介紹

    這篇文章介紹了ASP.NET?Core?MVC自定義Tag?Helpers的用法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-02-02
  • ASP.NET Core根據(jù)環(huán)境變量支持多個(gè) appsettings.json配置文件

    ASP.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-08
  • Visual Studio Debug實(shí)戰(zhàn)教程之?dāng)帱c(diǎn)操作

    Visual 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-09
  • ASP.NET Core WebAPI實(shí)現(xiàn)本地化(單資源文件)

    ASP.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
  • asp.net直接Response輸出WML頁面示例代碼

    asp.net直接Response輸出WML頁面示例代碼

    本例實(shí)現(xiàn)直接Response輸出WML頁面,具體代碼如下,有需要的朋友可以和參考下
    2013-08-08

最新評論