【问题标题】:How to display kendoDropDownList by calling ajax in ASP.NET MVC framework?如何通过在 ASP.NET MVC 框架中调用 ajax 来显示 kendoDropDownList?
【发布时间】:2021-07-24 04:33:33
【问题描述】:

我有一个剑道 kendoDropDownList。我试图通过调用 Ajax 来填充下拉值。不幸的是,每次我单击下拉列表时,它都会调用 Action 并返回值,但下拉列表不显示列表。我用一个简单的字典测试了几次,它适用于测试数据(显示在代码中),但是当我用真实数据填充相同的数据时,它没有显示下拉值。如果我做错了什么,你能看看吗?

行动:

public ActionResult SubmitterActionTypes(int id)
        {
            var types = ServiceProvider.SupplementalDataService.SubmitterActionTypes()
                .OrderBy(x => x.Name);

   
            Dictionary<int, string> typesDic = new Dictionary<int, string>();

            // Following Test code works fine and drop down populates with values...
            /*typesDic.Add(1, "Item 1");
            typesDic.Add(2, "Item 2");
            typesDic.Add(3, "Item 3");
            typesDic.Add(4, "Item 4");*/

            // Following code does not work and dropdown does not show any result
            foreach(var type in types)
            {
                if (type.Name!=null && type.Name != "None")
                {
                    typesDic.Add(Convert.ToInt32(type.SubmitterActionRequiredTypeID), type.Name.ToString());
                }
            }

            return PartialView("JsonResult", typesDic.ToList()); 
        }

阿贾克斯:

var submitterActionRequired = $("#submitterActionRequired").kendoDropDownList({
        optionLabel: "Select User Assignee...",
        dataTextField: "Value",
        dataValueField: "Key",
        height: 310,
        Width: "900px",
        dataSource: {
            transport: {
                read: function (options) {
                    $.ajax({
                        url: "/Submission/SubmitterActionTypes",
                        dataType: "JSON",
                        data: {
                            id: EntityOrganizationID
                        },
                        success: function (result) {
                            // notify the data source that the request succeeded
                            options.success(result);
                        },
                        error: function (result) {
                            // notify the data source that the request failed
                            options.error(result);
                        }
                    });
                }
            }
        }
    }).data("kendoDropDownList");

【问题讨论】:

    标签: c# ajax asp.net-mvc action kendo-dropdown


    【解决方案1】:

    我觉得actionResult的返回类型应该是这样的:

    返回 Json(分配)

    此上下文中的 ParttailView 不起作用。

    【讨论】:

    • 我尝试了这种方法,但仍然没有结果下拉。我注意到将操作更改为 JsonResult 并返回 JAson(assignees),回调根本不起作用——这意味着一旦我单击下拉菜单,它就不会转到操作。这是我尝试过的代码 public JsonResult AssigneesForSubmission(int?id, bool?allowUnclaimed) {... return Json(assignees); }
    • 任何其他想法如何解决这个问题?
    • 如果调用没有执行操作,这意味着您错过了类似的东西,检查 url 中的控制器名称是否是 SubmissionController,或者操作的类型(获取或发布)。跨度>
    • 在返回DataSourceRequest这样的场景中,有一个特殊的兼容返回对象从Kendo,试试吧 public ActionResult AssigneesForSubmission([DataSourceRequest] DataSourceRequest request, int? id, bool?allowUnclaimed) { var wfs = DIResolver.GetConcreteInstanceOf(); var assignees = wfs.GetWorkflowAssigneeList(id.GetValueOrDefault(), UserContext.UserID, allowUnclaimed.GetValueOrDefault());返回 Json(assignees.ToDataSourceRequest(request)); }
    • 我用你的代码试过了,但是没有用。如果让您感到困惑,请忽略我之前的评论。 Kendo 不会在这里返回任何内容,实际上在 onload 时我们正在尝试将数据绑定到下拉列表,这是一个 Kendo。上面提到的这种方法适用于另一个下拉菜单,但它不适用于这个下拉菜单。这是因为我们返回列表的方式吗?
    猜你喜欢
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 2010-12-05
    • 2018-09-22
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    相关资源
    最近更新 更多