簡體   English   中英

帶有遠程數據和asp.net的jQuery select2

[英]jQuery select2 with remote data and asp.net

我正在使用select2庫替換選擇框。 我重新排列了示例7,您可以在Select2庫頁面上找到它(向下滾動ID $("#e7").select2等...)。 我做了自己的通用處理程序,該處理程序返回序列化的json數據:

GetData.asxh視圖:公共類GetData:IHttpHandler {public bool IsReusable {get {return false; }}

    public class RecipesList
    {
        public int total { get; set; }
        public List<TopRecipeTable> recipes { get; set; }

        public RecipesList() { }

        public RecipesList(int total, List<TopRecipeTable> recipes)
        {
            this.total = total;
            this.recipes = recipes;
        }
    }

    private string GenerateJsonSerializedObject(int languageId, string orderBy)
    {            
        RecipesList recipeList = new RecipesList(15, DBDataBase.GetTopRecipesByNumberOfRecipes(languageId, 15));
        return new JavaScriptSerializer().Serialize(recipeList);
    }

    public void ProcessRequest(HttpContext context)
    {
        int languageId;            
        bool languageParsed = int.TryParse(context.Request["languageId"], out languageId);
        string orderBy = (string)context.Request["orderBy"];

        if (languageParsed && orderBy != string.Empty)
        {enter code here
            context.Response.ContentType = "application/json";
            var jsonValue = GenerateJsonSerializedObject(languageId, orderBy);
            context.Response.Write(jsonValue);
        }
    }

這個通用處理程序返回正確的json格式(我使用此URL對其進行了檢查)。 我的結果(json)也與上述頁面示例中的結果相同。 但是這個jQuery之后不再觸發了。

我的劇本:

$(document).ready(function () {
        $("#e8").select2({
            placeholder: "Search for a recipe",
            //minimumInputLength: 1,
            ajax: {                               
                url: "/Handlers/GetData.ashx",
                dataType: 'jsonp',
                data: function (term, page) {
                    return {
                        languageId: 1,
                        orderBy: "TA"
                    };
                },
                results: function (data, page) {
                    alert(data.total);
                    var more = (page * 10) < data.total; // whether or not there are more results available

                    // notice we return the value of more so Select2 knows if more results can be loaded
                    return { results: data.recipes, more: more };
                }
            },
            formatResult: movieFormatResult, // omitted for brevity, see the source of this page
            formatSelection: movieFormatSelection, // 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
        });
    });

我嘗試在原始示例中編寫相同的alert(data.total) ,但它在我的版本中不起作用。 因此,我使用了正確的json格式,jQuery調用了我的通用處理程序,還接收了參數languageId ...,並且還返回了正確的json格式,但沒有。 我不知道我是否在這里遺漏了一些東西,因為我確信該東西也可以與通用處理程序一起使用。 我希望我能提供有關我的問題的足夠信息。

I can also add my result in jquery .ajax error handler : 
xhr.status = 200
ajaxOptions = parsererror
horwnError = SyntaxError : invalid label
If this is any helpful information

這個問題已經很老了,因此可以肯定的是,您現在已經有了解決方案...但是:

刪除所有這些功能:

formatResult:movieFormatResult formatSelection:movieFormatSelection dropdownCssClass:... escapeMarkup:....

您沒有提供那些功能來格式化數據嗎? 所有這些僅在您自定義項目下拉列表時才需要。

您正在返回data.recipes-必須是{Text:“”,Id:“”}}的數組,或者需要從此處返回的內容中構建它。

首先,讓它只處理包含基本數據的基本列表...然后從那里開始。

此外,當您開始工作時,請嘗試使用WebApi或ServiceStack而不是IHttpHandler來處理數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM