locked
Select 2 not populated with ajax and WebAPI RRS feed

  • Question

  • User799396372 posted

    i have used Web API Controller for poulating a select2 with departements but it's not poulating. when i use F12,  i have seen in network response:

    Message No HTTP resource was found that matches the request URI 'http://localhost:5709/api/ResultApi/?match[_type]=query&comptChargeId=&comptAnalId=&pageSize=5'.
    MessageDetail

    No action was found on the controller 'ResultApi' that matches the request.

    there is my controller Api

    public class ResultApiController : ApiController
        {
            UniteOfWorkBLL uniteOfWorkBll = new UniteOfWorkBLL();
            //recuperer les structures paginées pour les remplire
            public ResultList<ModelDto> GetStructures(string match, int? comptChargeId = null, int? comptAnalId = null, int page = 1, int pageSize = 5)
            {
                int totalCount;
                if (page < 1)
                    page = 1;
                if (page > 20)
                    page = 20;
    
                var query = uniteOfWorkBll.StructureBLL.GetAllFiltrerdOrderedPaged(out totalCount, match, "", pageSize * (page - 1), pageSize)
                    .Select(s => new ModelDto
                    {
                        id = s.Id.ToString(),
                        text = System.Globalization
                                    .CultureInfo.CurrentCulture
                                    .TextInfo.ToTitleCase(s.Designation.ToLower() + "(" + s.CodeStruct + ")")
    
                    }).ToList();
    
                return new ResultList<ModelDto> { items = query, totalCount = query.Count};
    
            }
    
            
        }
    }
    

    my java script code

    $('#SelectedStructure').select2({
                    placeholder: "--Selectionnez--",
                    width: '100%',
                    minimumInputLength: 0,
                    allowClear: true,
                    ajax: {
                        url: '/api/ResultApi/',
                        dataType: 'json',
                        type: 'Get',
                       
                        quietMillis: 100,
                        data: function (term, comptChargeId, comptAnalId, page) {
                            return { match: term, comptChargeId: "", comptAnalId: "" , page: page, pageSize: 5};
                        },
                        results: function (data, page) {
                            var more = page * 5 < data.totalCount;
                            return { results:data.items, page: page, more: more };
                        }
                    }
                });

    Monday, October 14, 2019 10:53 AM

Answers

  • User61956409 posted

    Hi Beginner32,

    MessageNo HTTP resource was found that matches the request URI 'http://localhost:5709/api/ResultApi/?match[_type]=query&comptChargeId=&comptAnalId=&pageSize=5'.

    If you'd like to add additional query parameters, you can try to modify [the ajax.data option](https://select2.org/data-sources/ajax#request-parameters) like below.

    data: function (params, comptChargeId, comptAnalId, page) {
    
        var query = {
            match: params.term,
            comptChargeId: "",
            comptAnalId: "",
            page: page,
            pageSize: 5
        }
    
        return query;
    }

    With Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 15, 2019 6:31 AM