除了上面的示例代码,我还发现了这个,它在 .net-core 的 listviews 上有更多文档:
https://github.com/telerik/kendo-ui-core/blob/master/docs/api/javascript/ui/listbox.md
当更改 C# 列表中的返回对象时,我将数据源绑定到 AJAX 方法的响应中,我还注意到它是什么类型并不重要,只要属性名称与列表视图的 dataTextField 和 dataValueField 匹配.
从列表视图中正确获取所选项目的解决方案可以与最初绑定的选项和已在列表视图之间移动的选项一起使用(如问题所示,不需要对列表视图进行更改):
//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());
}
这是绑定列表视图的 AJAX 方法的最小示例。在 document.ready 函数中包含对此方法的调用(thingId 是某个对象的 ID,该对象将在列表中包含子对象,然后将绑定到列表视图)。在我的情况下,我使用的是打字稿,您可能需要根据需要将其中的一些转换为基本的 javascript(它非常接近,但可能需要一些细微的更改 - 如“$”字符所示,您将还需要为此包含 jquery):
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
最后,你的控制器方法应该是这样的:我将包含一个伪类并用数据填充它。返回对象是“响应”变量,无论您的列表名称是什么,都可以在数据源中像这样访问:response.listname。最后,无论这些列表中的对象类型是什么,这些对象上的属性名称都必须与列表视图的 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);
}