2

Select2 jquery プラグイン (MVC3、Razor、および Knockout JS を使用) と、ajax 呼び出しに渡したいカスタム パラメーターに問題があります。

私はこのコードを持っています:

$('#QuickSearchPresentation').select2({
        placeholder: "Search for a movie",
        minimumInputLength: 1,
        ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
            url: '@Url.Action("QuickSearchMainContainerFromPresentationByIdFolders", "DataCenter", new { amount = 10 })',
            contentType: 'application/json',
            dataType: 'json',
            type: 'POST',
            quietMillis: 400,
            data: function (term, page) {
                return ko.toJSON({
                    term: term,
                    idFolders: vm.Container.Catalogs
                });
            },
            results: function (data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                console.log('results', data);
                return {results: data};
            }
        },
        initSelection: function(element, callback) {

            console.log('element', element);

        },
        formatResult: containerQuickSearchResult, // omitted for brevity, see the source of this page
        formatSelection: containerQuickSearchResult,  // omitted for brevity, see the source of this page
        dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
        escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
    });

vm はノックアウト js からの私のモデルであり、カタログは int の配列です。

問題は、idFolders 配列をサーバー上のアクションに渡すことができないということです。サーバーは「値を null にすることはできません。パラメータ名:ソース」(コントローラのアクションで配列がnullになるため)。考えられるすべての方法を試してみましたが、正しい方法が見つかりません。

私はあなたがそれで私を助けてくれることを願っています.

ありがとう、ゴンザロ。

4

1 に答える 1

0

jQuery ajax 呼び出しで、 data プロパティを JSON.stringify() でラップしてみてください。このような:

data: function (term, page) {
    return JSON.stringify(ko.toJSON({
        term: term,
        idFolders: vm.Container.Catalogs
    }));
},

データを JSON.stringify でラップすると、MVC が JSON を正しく処理するようになることに気付きました。

于 2013-10-18T04:08:36.647 に答える