淘汰赛剑道的DropDownList阿贾克斯observableArray获得选择的项目名称 [英] Knockout-Kendo dropdownlist Ajax observableArray get selected item name

查看:143
本文介绍了淘汰赛剑道的DropDownList阿贾克斯observableArray获得选择的项目名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是MVC 5,我用下面的淘汰赛,剑道下拉列表:

My application is MVC 5, I use the following Knockout-kendo dropdown list:

 <input data-bind="kendoDropDownList: { dataTextField: 'name', dataValueField: 'id', data: foodgroups, value: foodgroup }" />

   var ViewModel = function () {
        var self = this;
         this.foodgroups = ko.observableArray([
         { id: "1", name: "apple" },
         { id: "2", name: "orange" },
         { id: "3", name: "banana" }
         ]);
        var foodgroup =
        {
            name: self.name,
            id: self.id
        };

        this.foodgroup = ko.observable();
        ko.bindingHandlers.kendoDropDownList.options.optionLabel = " - Select -";
        this.foodgroup.subscribe(function (newValue) {
            newValue = ko.utils.arrayFirst(self.foodgroups(), function (choice) {
                return choice.id === newValue;
            });

            $("#object").html(JSON.stringify(newValue));
           alert(newValue.name);
        });
    };
    ko.applyBindings(new ViewModel());

由于它的伟大工程,这个答案<一个href=\"http://stackoverflow.com/questions/35397652/knockout-kendo-dropdownlist-get-text-of-selected-item\">Knockout剑道DROPDOWNLIST获得所选项目的文本

然而,当我改变了observableArray阿贾克斯:

However when I changed the observableArray to Ajax:

       this.foodgroups = ko.observableArray([]),
                $.ajax({
                    type: "GET",
                    url: '/Meals/GetFoodGroups',

                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        self.foodgroups(data);
                    },
                    error: function (err) {
                        alert(err.status + " : " + err.statusText);
                    }
                });

控制器 - 从MS SQL服务器获得的表:

Controller - get the table from ms sql server:

 public JsonResult GetFoodGroups()
            {
                var data = db.FoodGroups.Select(c => new
                {
                    id = c.FoodGroupID,
                    name = c.FoodGroupName
                }).ToList();

                return Json(data, JsonRequestBehavior.AllowGet);
            }

在这里输入的形象描述

当我提醒项目名称我得到这个错误

I get this error when I alert the item name

 Unable to get property 'name' of undefined or null reference

是什么,从使用Ajax硬编码数组项之间的差。

What is the difference between hardcoding the array items from using Ajax.

推荐答案

在'ID'字段的字符串数据类型硬codeD阵列。

The 'id' field has string datatype in hard coded array.

在这里输入的形象描述

在'ID'字段的数量数据类型在阿贾克斯阵。

The 'id' field has number datatype in ajax array.

在这里输入的形象描述

所以,ID字段有两个数组不同的数据类型。然而,在条件你使用的 === 运营商,因此会检查以及数据类型

So, the 'id' field has different datatypes in both arrays. However in-condition you have used === operator so it checks value as well as datatype.

在这里输入的形象描述

有关AJAX数组值是相同的,但它的数据类型是不同的,所以它不会返回结果。

For ajax array value is same but its datatype is different so its not returning result.

让我知道,如果任何问题。

Let me know if any concern.

这篇关于淘汰赛剑道的DropDownList阿贾克斯observableArray获得选择的项目名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆