0

Im having terrible trouble with getting the ajax attached to the select2 to fire. The select2 box is rendering correctly, however there is no call made to the controller when searching .

I have taken this from a working example in another project...which makes it all the more confusing

here are the JavaScript and css files in the layout header

   <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/select2/4.0.3/js/select2.full.js"></script>

here is my input

<input type="hidden" id="companyname" name="companyname" style="width: 100%;" />

here is my ajax call

   $(document).ready(function () {
    $("#companyname").select2({
        placeholder: "Type to find a Provider",
        theme: "bootstrap",
        allowClear: true,
        minimumInputLength: 2,
        ajax: {
            cache: false,
            dataType: "json",
            typ8e: "GET",
            url: "@Url.Action("/CompanyHouse/SearchCompany")",
            data: function(params){
                return { companyname: params.term };
            },
            processResults: function (data, page) {
                return { results: data };
            }
        }
        //    formatResult: contractFormatResult,
        //formatSelection: contractFormatSelection,
        //escapeMarkup: function (m) { return m; }
    });
    });

1 Answer 1

1

Two things:

  • You have a typo on ajax options type:"GET"
  • The correct way to use Url.Action is '@Url.Action("YourAction","YourController")'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.