简体   繁体   English

在KENDO网格中没有填充DropDown

[英]DropDown is not populating in KENDO Grid

i am not able to populating data in KENDO dropdown where data is coming form database. 我无法在KENDO下拉列表中填充数据,其中数据来自数据库。 here is my code for KENDO DROPDOWN: 这是我对KENDO DROPDOWN的代码:

function positionDropDownEditor(container, options) {
    $('<input name="Size" required data-bind="value:' + options.field + '"/>')
        .appendTo(container)
        .kendoDropDownList({
         autoBind: false,
         dataTextField: "Name",
         dataValueField: "Id",
           dataSource: {
                        transport: {
                            read: {
                                dataType: "json",
                                url: "/Employee/AllPosition",
                            }
                        }
                    } 
        });
}

And the controller from where data is coming from: 以及来自数据的控制器:

public JsonResult AllPosition()
{
    EmployeeService employeeService = new EmployeeService();
    List<Position> positions= employeeService.GetAllPosition();
    return Json(positions);
}

whats wrong here that data are not populating inside the dropdown? 这是怎么回事,下拉列表中没有填充数据? plz explain including "container, options" and what value they contain and why we need to use? plz解释包括“容器,选项”的内容,以及它们包含的值以及我们为什么要使用?

Thank you 谢谢

After concerning whole day and try, i solved the problem. 经过一整天的尝试,我解决了这个问题。 i hope this will help others. 我希望这会帮助别人。 so silly, it took my whole day. 真傻,花了我一整天。 finally i found the problem in AllPosition(). 终于我在AllPosition()中发现了问题。 here the return type will be sting. 这里的返回类型将是字符串。 the code will be: 该代码将是:

public string AllPosition()
    {
        EmployeeService employeeService = new EmployeeService();
        List<Position> positions= employeeService.GetAllPosition();
        var x = JsonConvert.SerializeObject(positions);      
        return x;
    }

Dont ask me why return "JsonConvert.SerializeObject(positions)" not return "json(positions)". 不要问我为什么返回“ JsonConvert.SerializeObject(位置)”而不返回“ json(位置)”。 i had to convert Return type from JsonResult to string. 我不得不将返回类型从JsonResult转换为字符串。

Thanks all for concern and tried to help. 感谢大家的关注并竭诚为您服务。

You should remove the autoBind flag 您应该删除autoBind标志

Controls whether to bind the widget to the data source on initialization. 控制是否在初始化时将小部件绑定到数据源。

EDIT: 编辑:

I'm sorry, I totaly overlooked that it is already false . 抱歉,我完全忽略了它已经是false Anyways... 无论如何...

Did you check in the browser's debugger if you get the right response from the server at all? 是否从服务器获得了正确的响应,是否签入了浏览器的调试器? It might be that some properties could not be serialized correctly and the server returns null. 某些属性可能无法正确序列化,并且服务器返回null。

Add this method to your Global.asax.cs to catch more errors: it will print errors that are otherwise no where visible... if the error is on the server 将此方法添加到您的Global.asax.cs捕获更多错误:它将打印错误,否则该错误在服务器上看不到...如果错误在服务器上,

protected void Application_EndRequest() {
    if (Context.AllErrors != null) {
        System.Diagnostics.Debugger.Break();
        foreach (var ex in Context.AllErrors) {
            System.Diagnostics.Debug.WriteLine(ex.Message);
        }
    }
} 

Change your Controller to do this: 更改您的控制器以执行此操作:

public JsonResult AllPosition()
{
    EmployeeService employeeService = new EmployeeService();
    List<Position> positions= employeeService.GetAllPosition();
    return Json(positions, JsonRequestBehavior.AllowGet);
}

EDIT: 编辑:

Also try changing your JavaScript code to look like this: 另外,尝试将JavaScript代码更改为如下所示:

  $('<input name="Size"  data-text-field="Name" data-value-field="Id" required data-bind="value:' + options.field + '"/>')
    .appendTo(container)
    .kendoDropDownList({
     autoBind: true,
       dataSource: {
                      transport: 
                      {                         
                           read: {dataType: "json", 
                           url: "@Url.Action("Employee", "AllPosition")", type: "GET" }
                      }
                } 
    });

If this view is in an area and you find your not getting to the controller then you can add the area in the read like so: 如果此视图在某个区域中,但您发现您未进入控制器,则可以像这样添加区域:

 url: "@Url.Action("Employee", "AllPosition")",new {@area = "AreaName"} type: "GET" }

As an alternative you could use JSON.NET to return ActionResult 或者,您可以使用JSON.NET返回ActionResult

by Sven Grosen 通过Sven Grosen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM