Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually i had implement the multiple values jquery autocomplete textbox.if the option or item not found in the list i want to add that option to autocomplete box

Autocomplete.cshtml:

Razor
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Select2 with Ajax</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
    <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.2/jquery.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
</head>
<body>
    <div class="container">
        <div class="form-group">
            <label for="Select Country">Select Country</label>
            <select multiple="multiple" class="chose-country form-control"></select>
        </div>
    </div>
    <script>
        $(document).ready(function () {
            $(".chose-country").select2({
                ajax: {
                    url: '/Home/GetEmployeeList',
                    width: 'resolve',
                    data: function (params) {
                        return {
                            q: params.term// search term
                        };
                    },
                    processResults: function (data) {
                        return {
                            results: data.items
                        };
                    },
                    minimumInputLength: 2,
                    width: 'resolve'
                }
            });

        });
    </script>
</body>
</html>


Select2Model:

C#
public class Select2Model
   {
       public string id { get; set; }
       public string text { get; set; }
   }


HomeController:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication3.Models;

namespace WebApplication3.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult autocomplete()
        {
            return View();
        }
        public ActionResult GetEmployeeList(string q)
        {

            var list = new List<Select2Model>();

            list.Add(new Select2Model()
            {
                text = "India",
                id = "101"
            });
            list.Add(new Select2Model()
            {
                text = "Srilanka",
                id = "102"
            });
            list.Add(new Select2Model()
            {
                text = "Singapore",
                id = "103"
            });

            if (!(string.IsNullOrEmpty(q) || string.IsNullOrWhiteSpace(q)))
            {
                list = list.Where(x => x.text.ToLower().StartsWith(q.ToLower())).ToList();
            }
            return Json(new { items = list }, JsonRequestBehavior.AllowGet);
        }
    }
}



Please help me.
thanks && Regards

What I have tried:

Actually i had implement the multiple values jquery autocomplete textbox.if the option or item not found in the list i want to add that option to autocomplete box
Posted
Updated 25-Dec-19 3:29am

1 solution

To add a new option to the select2 element you would use tagging
Dynamic option creation | Select2 - The jQuery replacement for select boxes[^]

Now if this is going to be a permanent addition you have an issue; as your controller is using a hard-coded list of countries to populate this box, and you can't just add to this code.

Perhaps you could move your countries to a fixed data source (e.g. SQL, XML, or a text file) which has the IDs, Text, and an "active" bit; just populate the displayed list with only those countries which are active.
And if a different country is submitted, you could update the active bit of the fixed data source for future usage
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900