Kendo ui Kendo listview-单击在listview之间移动的元素会产生不同的发送方对象结构

Kendo ui Kendo listview-单击在listview之间移动的元素会产生不同的发送方对象结构,kendo-ui,kendo-listview,Kendo Ui,Kendo Listview,我发现一个问题,如果我向列表框添加新选项,当我在新的列表视图中单击这些新移动的选项时,event.sender不再具有相同的对象结构 //This will be the type of object I'm putting into the list that's //going into the object I'm returning public MyClass { public int Id {get; set;} //so we change the listview'

我发现一个问题,如果我向列表框添加新选项,当我在新的列表视图中单击这些新移动的选项时,event.sender不再具有相同的对象结构

//This will be the type of object I'm putting into the list that's 
//going into the object I'm returning

public MyClass {
    public int Id {get; set;}  //so we change the listview's dataValueField to Id
    public string Name {get; set;} //we change the listview's dataTextField to Name
}

//And this will be the overall type of object that will hold the list, will get
//returned, becoming the "response" in the AJAX .done:
public class MyReturnObject {
    public List MyList {get; set;}  //so we change the ListView's datasource to response.MyList
    //It may become myList, so you may need to look at the response object in the debugger.
}


[HttpPost]
public JsonResult MyControllerMethod(int id)
{
    //Create the return object, give it a new list, add things to the list
    //so the ListView can be bound:
    var myReturnObject = new MyReturnObject();
    myReturnObject.Mylist = new List();
    var object1 = new MyClass { Id = 1, Name = "Hello World" };
    var object2 = new MyClass { Id = 2, Name = "Hello again" };
    myReturnObject.MyList.Add(object1);
    myReturnObject.MyList.Add(object2);

    //Convert the return object and everything in it to Json and return it to
    //The AJAX method:
    return Json(myReturnObject);
}
我使用ajax事件将数据绑定到Kendo listview(这是一个在文档就绪时触发的方法):

您可以看到,它将“myJavascriptMethod(event)”绑定为变更事件处理程序

下面是我如何访问myJavascriptMethod(事件)中的事件数据:


问题是,如果我修改选项(我使用“transferFrom”和“transferTo”在两个剑道列表视图之间传输选项),event.sender.\u目标为空。我很难弄清楚在任何情况下都可以使用的关键点是什么。

除了上面的示例代码,我还发现了这个,它在.net core的ListView上有更多的文档:

在对AJAX方法的响应中,我将数据源绑定到C#List中,当更改返回对象时,我还注意到,只要属性名与listview的dataTextField和dataValueField匹配,返回对象的类型实际上并不重要

从listview中正确获取所选项目的解决方案是(如问题所示,无需更改listview):

下面是绑定listview的AJAX方法的一个最小示例。在document.ready函数中包含对此方法的调用(thingId是某个对象的id,该对象将在列表中包含子对象,然后该子对象将绑定到listview)。在我的例子中,我使用的是typescript,您可能需要根据需要将其中的一些转换为基本javascript(非常接近,但可能需要一些细微的更改-如“$”字符所示,您还需要为此包含jquery):

最后,以下是您的控制器方法在上述情况下的表现: 我将包含一个伪类并用数据填充它。return对象是“response”变量,在datasource:response.listname中访问任何列表名。最后,无论这些列表中的对象类型是什么,这些对象上的属性名称都必须与listview的dataTextField和dataValueField匹配

//This will be the type of object I'm putting into the list that's 
//going into the object I'm returning

public MyClass {
    public int Id {get; set;}  //so we change the listview's dataValueField to Id
    public string Name {get; set;} //we change the listview's dataTextField to Name
}

//And this will be the overall type of object that will hold the list, will get
//returned, becoming the "response" in the AJAX .done:
public class MyReturnObject {
    public List MyList {get; set;}  //so we change the ListView's datasource to response.MyList
    //It may become myList, so you may need to look at the response object in the debugger.
}


[HttpPost]
public JsonResult MyControllerMethod(int id)
{
    //Create the return object, give it a new list, add things to the list
    //so the ListView can be bound:
    var myReturnObject = new MyReturnObject();
    myReturnObject.Mylist = new List();
    var object1 = new MyClass { Id = 1, Name = "Hello World" };
    var object2 = new MyClass { Id = 2, Name = "Hello again" };
    myReturnObject.MyList.Add(object1);
    myReturnObject.MyList.Add(object2);

    //Convert the return object and everything in it to Json and return it to
    //The AJAX method:
    return Json(myReturnObject);
}

如果我转到另一个并将其传回,它将再次响应单击时的更改事件。另一个listview中的选项也是如此——如果它们是在数据绑定时添加的,它们将触发更改事件,但当移动到第一个listview时,将不再触发更改事件。总之,只有在数据绑定时(或更改事件处理程序绑定时)添加的选项才会在单击时触发更改事件。那么,当选项更改时,是否有方法刷新更改绑定?能否请您创建一个复制此行为的简化/虚拟数据?您所展示的代码可能正在运行,并不能真正帮助识别我正在尝试的问题。在cdn上查找all.min.css有点困难。我可能需要使用单独的版本的all.css.OK,它已加载。有一点准备工作要做,谢谢你的建议!哦,哇。好吧,最新版本显然不再有那个bug,或者从最简单的代码开始消除了我的问题。我将在评论中发布一个指向fiddler的链接,这样您就可以看到它在工作,尽管您对此很感兴趣。您可以在同一个AJAX帖子中包含对所有listview的绑定,您可以向返回对象添加多个列表,然后将每个listview绑定到不同的列表(这就是我正在做的).我不使用上面提供给控制器方法的id参数,因为这将涉及数据库访问,这与只将数据获取到listview中的问题无关,但您明白了。
//reformatted this as Javascript
//for typescript it was of this format:
//static myTypeScriptEvent(event){

function myJavascriptEvent(event){
    //This line was the fix / is key to the question:
    var dataItem = event.sender.dataItem(event.sender.select());
}
function bindListView( id: thingId ) {
    var dataToPost = {
         id: id
    };

    $.post('myUrl/myController/controllerMethod', dataToPost)
    .done(function (response, status, jqxhr) {
       $('#' + myListBoxId').kendoListBox({
          dataSource:  response.myList,
          connectWith: theOtherListBoxId,
          dropSources: [theOtherListBoxId],
          toolbar: {
                        position: "right",
                        tools: ["transferAllFrom", "transferAllTo", 
                                      "transferFrom", "transferTo"]
          },
          change:  function (event) {
                myJavascriptMethod(event);
          },
          dataTextField: 'Id',
          dataValueField: 'Name'
       });  //end of listView bind
      }); //end of $.post().done() function
}  //end of bindListView() function
//This will be the type of object I'm putting into the list that's 
//going into the object I'm returning

public MyClass {
    public int Id {get; set;}  //so we change the listview's dataValueField to Id
    public string Name {get; set;} //we change the listview's dataTextField to Name
}

//And this will be the overall type of object that will hold the list, will get
//returned, becoming the "response" in the AJAX .done:
public class MyReturnObject {
    public List MyList {get; set;}  //so we change the ListView's datasource to response.MyList
    //It may become myList, so you may need to look at the response object in the debugger.
}


[HttpPost]
public JsonResult MyControllerMethod(int id)
{
    //Create the return object, give it a new list, add things to the list
    //so the ListView can be bound:
    var myReturnObject = new MyReturnObject();
    myReturnObject.Mylist = new List();
    var object1 = new MyClass { Id = 1, Name = "Hello World" };
    var object2 = new MyClass { Id = 2, Name = "Hello again" };
    myReturnObject.MyList.Add(object1);
    myReturnObject.MyList.Add(object2);

    //Convert the return object and everything in it to Json and return it to
    //The AJAX method:
    return Json(myReturnObject);
}