Monday, 2 September 2019

Bootstrap Groupwise Multi select checkbox in core(2.1) & MVC6



View Model :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Rendering;


namespace WebApplication2.Models
{
    public class GroupVM
    {
        public GroupVM()
        {
            Lgroup = new List<int>();
        }
        public List<int> Lgroup { get; set; }
    }
}

Controller :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WebApplication2.Models;


namespace WebApplication2.Controllers
{
    public class GajananController : Controller
    {
       
        [HttpGet]
        public IActionResult Cgroup()
        {
            GroupVM vm = new GroupVM();
            return View();
        }
        [HttpPost]
        public IActionResult Cgroup(GroupVM vm)
        {
            return View();
        }
        [HttpGet]
        public IActionResult Gets()
        {
            List<EMP1> lst1 = new List<EMP1>() {
                 new EMP1 { DID=1, NAME="A",EID=1 },
                 new EMP1 { DID=1, NAME="B",EID=2 },
                 new EMP1 { DID=2, NAME="C" ,EID=3},
                 new EMP1 { DID=2, NAME="D" ,EID=4},
                 new EMP1 { DID=3, NAME="E" ,EID=5},
                 new EMP1 { DID=3, NAME="F" ,EID=6}
            };
            List<DEPT1> lst = new List<DEPT1>() {
                 new DEPT1 { DID=1, DNAME="X", LEMP=lst1.Where(m=>m.DID==1).ToList() },
                 new DEPT1 { DID=2, DNAME="Y",LEMP=lst1.Where(m=>m.DID==2).ToList() },
                 new DEPT1 { DID=3, DNAME="Z" ,LEMP=lst1.Where(m=>m.DID==3).ToList()}
            };
            return Json(JsonConvert.SerializeObject(lst.ToList()));

        }
    }
}

View :

@model WebApplication2.Models.GroupVM

@{
    ViewData["Title"] = "Cgroup";
}

<h1>Cgroup</h1>


<div class="row">
    <div class="col-md-4">
        <form asp-action="Cgroup">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label class="control-label col-4" asp-for="Lgroup"></label>
                <div class="col-4">
                    <select multiple asp-for="Lgroup" class="form-control"></select>
                </div>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
    <script type="text/javascript">
        $(function(){
            $.ajax({
            url:'@Url.Action("Gets")',
            type: 'Get',
            datatype: 'json',
            cache: false,
            success: function (data) {
                var data2 = JSON.parse(data);
                var data3 = [];
                    for (var i = 0; i < data2.length; i++) {
                    var group = { label: data2[i].DNAME , children: [] };
                    for (var j = 0; j < data2[i].LEMP.length; j++) {
                        group['children'].push({
                            label: data2[i].LEMP[j].NAME ,
                            value: data2[i].LEMP[j].EID
                        });
                        }
                 data3.push(group);
                }
                $('#Lgroup').multiselect('dataprovider', data3);
            },
            error: function (t) {
                alert(t.responseText);
            }
            });
           
        });
    </script>
}

Layout View :

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - WebApplication2</title>

    <environment include="Development">
        <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
        
        <link href="~/themes/base/jquery.ui.all.css" rel="stylesheet" />
        <link href="~/themes/base/jquery.multiselect.css" rel="stylesheet" />
        <link href="~/themes/base/jquery.ui.datepicker.css" rel="stylesheet" />
        <link href="~/css/DataTables/css/jquery.dataTables.css" rel="stylesheet" />
        <link href="~/css/DataTables/css/jquery.dataTables_themeroller.css" rel="stylesheet" />
        <link href="~/themes/base/jquery.ui.dialog.css" rel="stylesheet" />
        <link href="~/themes/base/jquery.ui.autocomplete.css" rel="stylesheet" />
        <link href="~/css/bootstrap-multiselect.css" rel="stylesheet" />
    </environment>
    <environment exclude="Development">
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css"
              asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute"
              crossorigin="anonymous"
              integrity="sha256-eSi1q2PG6J7g7ib17yAaWMcrr5GrtohYChqibrV7PBE="/>
    </environment>
    <link rel="stylesheet" href="~/css/site.css" />
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
            <div class="container">
                <a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">WebApplication2</a>
                <button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                        aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
                    <ul class="navbar-nav flex-grow-1">
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
                        </li>
                    </ul>
                </div>
            </div>
        </nav>
    </header>
    <div class="container">
        <partial name="_CookieConsentPartial" />
        <main role="main" class="pb-3">
            @RenderBody()
        </main>
    </div>

    <footer class="border-top footer text-muted">
        <div class="container">
            &copy; 2019 - WebApplication2 - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
        </div>
    </footer>

    <environment include="Development">
        <script src="~/lib/jquery/dist/jquery.js"></script>
        <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
        <script src="~/Multiselect/datepicker.js"></script>
        <script src="~/Multiselect/core.js"></script>
        <script src="~/Multiselect/widget.js"></script>
        <script src="~/Multiselect/multiselect.js"></script>
        <script src="~/js/DataTables/jquery.dataTables.js"></script>
        <script src="~/Multiselect/jquery.ui.dialog.js"></script>
        <script src="~/js/jquery-ui.js"></script>
        <script src="~/js/bootstrap-multiselect.js"></script>
    </environment>
    <environment exclude="Development">
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
                asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
                asp-fallback-test="window.jQuery"
                crossorigin="anonymous"
                integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=">
        </script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.min.js"
                asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal"
                crossorigin="anonymous"
                integrity="sha256-E/V4cWE4qvAeO5MOhjtGtqDzPndRO1LBk8lJ/PR7CA4=">
        </script>
    </environment>
    <script src="~/js/site.js" asp-append-version="true"></script>

    @RenderSection("Scripts", required: false)
</body>
</html>




No comments:

Post a Comment