Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 759
  • 1.1k
  • 51.2k

Select2 and ajax not loading the data from sql database

Sep 23 2024 9:30 PM

Hello Team,

In my product mvc C# I 'm using bootrstrap select2 for  fetching category from category table and inserting the data into product table, but nothing seems to be working, any assistance will be appreciated.

 

 <div class="col-md-4">
                            <div class="form-group" id="txtdvCategory">
                                <label>Select Category<small class="text-muted">-- </small></label>
                                <select class="select2 form-control custom-select" style="width: 100%; height:36px;">
                                    <option>Select </option>
                                    <optgroup label="Item List">
                                        <option value="HL"></option>
                                        <option value="KL"></option>
                                    </optgroup>
                                </select>
                            </div>
                        </div>

 

 $("#txtdvCategory").select2({
                      //minimumInputLength: 1,
                      tags: [],
                      ajax: {
                          url: "/POS/getAllCategory",
                          dataType: 'json',
                          type: "GET",
                          quietMillis: 50,
                          data: function (term) {
                              return {
                                  term: term
                              };
                          },
                          results: function (data) {
                              return {
                                  results: $.map(data, function (item) {
                                      return {
                                          text: item.CategoryName,
                                          slug: item.slug,
                                          id: item.CategoryId
                                      }
                                  })
                              };
                          }
                      }
                  });

 

 public JsonResult getAllCategory()
        {
            var dataList = objHotelDbEntities.tblCategories.ToList();
            var modifiedData = dataList.Select(x => new CategoryViewModel
            {
                CategoryId = x.CategoryId,
                CategoryName = x.CategoryName
                
            }).ToList();
            return Json(modifiedData, JsonRequestBehavior.AllowGet);
        }


Answers (4)