Monday, 2 September 2019

Example of autocomplete in core(2.1) MVC6


Controller :

[HttpGet]
        public IActionResult Gets()
        {
                return Json(JsonConvert.SerializeObject(_iRepository.Gets().Select(x=>x.NAME).
                          ToList()));
        }

View :

@model WebApplication2.Models.EMPVM

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


<div class="row">
    <div class="col-md-4">
        <form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
           
            <div class="form-group">
                <label asp-for="NAME" class="control-label"></label>
                <input asp-for="NAME"  class="form-control" autocomplete="off" />
                <span asp-validation-for="NAME" class="text-danger"></span>
            </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 () {
            var data1 = new Array();
        $.ajax({
            url:'@Url.Action("Gets")',
            type: 'Get',
            datatype: 'json',
            cache: false,
            success: function (data) {
                var data2 = JSON.parse(data);
                for (var i = 0; i < data2.length; i++) {
                    data1.push(data2[i]);
                }
            },
            error: function (t) {
                alert(t.responseText);
            }
        });
        $("#NAME").autocomplete({
            minLength: 0,
            source: data1
        });


    });
    </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" />
    </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>
    </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