2

我有剑道下拉列表的问题。下拉返回对象对象,但我需要字符串。有人可以帮助我。这里代码。对不起我的英语,我来自俄罗斯

MyScript.js:(从管理控制器获取数据)

 $(document).ready(function () {
    $("#grid").kendoGrid({

        dataSource: {
            type: 'odata',
            serverSorting: true,
            serverFiltering: true,
            serverPaging: true,

            transport: {
                read: {
                    url: "/api/Admin",
                    dataType: "json",
                    contentType: "application/json",
                },
                create: {
                    url: "/api/Admin",
                    dataType: "json",
                    type: "POST"
                },
                update: {
                    url: function (AdminModel) {
                        return "/api/Admin/" + "?roles=" + AdminModel.Roles
                    },
                    dataType: "json",
                    type: "PUT"
                },
                destroy: {
                    url: function (AdminModel) {
                        return "/api/Admin/" + "?name=" + AdminModel.Name
                    },
                    dataType: "json",
                    type: "DELETE"
                },
                parameterMap: function (model, operation) {
                    if (operation !== "read" && model) {
                        return kendo.stringify(model);
                    }
                }
            },
            schema: {
                data: function (response) {
                    if (response.value !== undefined)
                        return response.value;
                    else {
                        delete response["odata.metadata"];
                        return response;
                    }
                },
                total: function (response) {
                    return response['odata.count'];
                },
                model: {
                    id: "ID",
                    fields: {
                        ID: { editable: false },
                        Name: { type: "string", editable: false, nullable: false, validation: { required: true } },
                        Roles: { type: "string", editable: false },
                        NewRole: { type: "string" },

                    }
                }
            }
        },


        height: 560,
        sortable: true,
        pageable: true,
        editable: "popup",
        columns: [
           { field: "ID", width: 50 },
           { field: "Name", title: "Name", width: 120 },
           { field: "Roles", title: "Roles", width: 120},
           { field: "NewRole", hidden:true,title: "NewRole", editor: RoleDropDownEditor, template: "#=NewRole#" },
           { command: ["destroy", "edit"], title: " ", width: "120px" }
        ]
    });
});
My Drop Down List(get data from Role controller)
function RoleDropDownEditor(container, options) {
    $('<input required  data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({

            dataTextField: "Role",
            dataValueGield: "Id",
            autoBind: false,
            dataSource: {
                transport: {
                    read: {

                        url: "/api/Role",
                        dataType: "json",
                        contentType: "application/json",

                    },
                    schema: {
                        model: {
                            fields: {
                                Id:{type:"string"},
                                Role: { type: "string" }

                            }
                        }
                    }
                }


            }

        });
}

控制器

Web API class Role.cs

    using MyCMS.Models;
using System.Web.Security;

namespace MyCMS.Controllers
{
    public class RoleController : ApiController
    {
        private UsersContext db = new UsersContext();

        public class MyRole
        {
            public string Id { get; set; }
            public string Role { get; set; }
        }

        // GET api/usermanage

        public List<MyRole> Get()
        {
            var result = new List<MyRole>();
            foreach (var role in Roles.GetAllRoles())
            {
                result.Add(new MyRole{ Id = role, Role = role});
            }
            return result;
        }
    }
}

看法

    <head>
    <title></title>
</head>

<div id="grid"></div>

@section Scripts{

    @Styles.Render("~/Content/kendo/2013.2.716/css")
    @Scripts.Render("~/bundles/kendo")
    @Scripts.Render("~/bundles/Admin")

}
4

3 回答 3

16

看看这篇文章和这个剑道文档。将 ValuePrimitive 设置为 'true' 为我解决了这个问题。

显然,如果模型中的字段为空,Kendo 会尝试将整个项目分配给该字段。将 ValuePrimitive 设置为 true 可以防止这种情况发生,它只是分配 value 字段。

于 2014-04-01T22:23:30.340 回答
0

您的下拉列表的数据源配置不正确。您已经在传输中定义了模式。这不是它需要的地方。你把一个右花括号放在错误的位置。以下是它的外观:

dataSource: {
  transport: {
    read: {
      url: "/api/Role",
      dataType: "json",
      contentType: "application/json"
    }
  },  // YOU WERE MISSING THIS
  schema: {
    model: {
      fields: {
        Id: { type: "string" },
        Role: { type: "string" }
      }
    }
  }
  // REMOVED AN INCORRECTLY PLACED } FROM HERE
}
于 2013-08-30T05:02:45.827 回答
-1

您所要做的就是将 kendo javascript 文件(如 kendo.all.min.js 和 kendo.default.min.js)添加到布局页面的 head 部分

于 2020-07-27T22:48:19.937 回答