Loading failed select2 searching

I am getting “Loading failed” error when I type characters in select2 textbox

  • i work with mvc , c# on visual studio 2015 community

  • code source in .cshtml
    to show textbox : @Html.Hidden(“cod_indem”)
    to apply select2 on this object (“cod_indem”) :

    $(function () {
    $("#cod_indem").select2(
    {
    width: “100%”,
    ajax: {
    type: “Get”,

              url: '@(Url.Action("GetAll", "Zpy_paye"))',
              cache: false,
    
              dataType: 'json',
    
              delay: 250,
              data: function (term, page) {
                  return {
                      match: term, // search term
                      pageIndex: page,
                      pageSize: 30
                  };
              },
              results: function (data, page) {
                  page = page || 1;
                  return {
                      results: data.items,
                      more: (page * 30) < data.total_count
    
                  };
              }
          },
    
          escapeMarkup: function (markup) { return markup; },
          minimumInputLength: 3,
          formatSelection: format,
          formatResult: format
    
      });
    

    });

    function format(data) {
    return data.text;

    }

///------- code of Select2DTO.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcPagingDemo.Models
{
public class Select2DTO
{
public Guid id { get; set; }
public string Code { get; set; }
public string text { get; set; }

}

}

///------- code of Select2Result.cs :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MvcPagingDemo.Models
{
public class Select2Result
{
public List items { get; set; }
public int total_count { get; set; }
}
}

///------- in controller :
public Select2Result GetAll(string match, int pageIndex = 1, int pageSize = 30)
{
int totalCount;
if (pageIndex <= 0) pageIndex = 1;
if (pageSize >= 30) pageSize = 30;
var zpy_indemnites = Get_All_indemnite_FilteredPaged(match, out totalCount, pageIndex - 1, pageSize);

        var zpy_indemnitesDTO = (from a in zpy_indemnites
                            select new Select2DTO { id = a.id , Code = "sss",  text = a.des_f }).ToList();

        return new Select2Result { items = zpy_indemnitesDTO, total_count = totalCount };
    }

//---------------------------------------------------

[HttpGet]
public List<zpy_indemnite> Get_All_indemnite_FilteredPaged(string match, out int totalCount, int pageIndex = 0, int pageSize = 10)
{
grh_paie_Entities context = new grh_paie_Entities();
context = ContextManager.Context;

        var query = context.zpy_indemnite.AsQueryable();

        //TODO: Check if the match parameter is not null or empty and add the necessary where condition.
        if (!string.IsNullOrEmpty(match))
            query = query.Where(c => c.des_f.Contains(match) );
        //TODO: Set the totalCount to the Total Count of zpy_indemnite in the Data Store.
        totalCount = query.Count();

        //TODO : Get the list of communes paged use the Skip and Take LINQ extension methods.
        var zpy_indemnites = query.OrderBy(c => c.des_f)
            .Skip(pageIndex * pageSize)
            .Take(pageSize)
            .ToList();

        return zpy_indemnites;
    }

please i need a helperror select2

Have you checked your browser’s console to see if there are any error messages logged there? You should also check the Network tab, because it should show the AJAX request being made and the response that’s returned. It’s possible the AJAX request is receiving an error response from the server.

Also, have you checked your server’s logs to see whether your server-side code is receiving the AJAX request and processing it properly? (I don’t know C#, so it’s hard for me to tell whether your code is responding in the format that Select2 expects.)

Hy Mr John
thanks alot for your helpccc

Hi, Meghassel–

I don’t have any experience with C#, so I can’t give you a definite answer. I did find this StackOverflow post that might help you. From what I can tell by reading that post, it seems you must create a bundle first, and then use @Scripts.Render() to load the bundle. I’m guessing that you haven’t created the bundle. I’m also guessing that your bundle should include all of the JavaScript files (jquery.validate.js, select2.js, and jquery.unobtrusive-ajax.js), and then you would have a single @Scripts.Render() call to load that bundle.

ccv

As I mentioned, I’m not well versed in C#. But I don’t think this is a Select2 issue per se; rather, it’s an issue with your C# bundle creation logic. Perhaps you should post this question on a C# forum (on StackOverflow or a similar site).