gpt4 book ai didi

c# - 从 ASP.Net MVC 4 中的 Kendo UI MultiSelect 获取 PrimaryId

转载 作者:行者123 更新时间:2023-12-02 17:45:43 25 4
gpt4 key购买 nike

我刚刚开始接触 ASP.Net 和 Kendo,非常感谢你们能够提供的任何帮助。我正在尝试使用 Kendo MultiSelect Widget 来显示员工列表(我已成功管理),并使用 Controller 上的 Json 方法从 View 模型返回结果集。我想要实现的是收集员工 ID 列表,以便我可以将它们发送回 Controller 进行处理。但是,我似乎只能获取列表中的值,而不能获取 Id。

有人可以给我一些关于如何实现这一目标的建议吗?

我的 ViewModel 看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TrainingKendoUI.Models;
using System.ComponentModel.DataAnnotations;

namespace TrainingKendoUI.ViewModels
{
public class EmployeeViewModel
{
#region Constructors

public EmployeeViewModel()
{

}

public EmployeeViewModel(TRAINING_EMPLOYEES model)
{
this.employeeId = model.EMPLOYEE_ID;
this.firstName = model.FIRST_NAME;
this.lastName = model.LAST_NAME;
this.email = model.EMAIL;
this.login = model.LOGIN;
this.fullName = model.FIRST_NAME + " " + model.LAST_NAME;
}

#endregion

#region Properties

public int employeeId { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
public string email { get; set; }
public string login { get; set; }
public string fullName { get; set; }

#endregion
}
}

我的 Json 方法如下所示:

    public JsonResult Employees_Read()
{
var model = db.TRAINING_EMPLOYEES;
var ViewModel = new List<EmployeeViewModel>();

foreach (var employee in model)
{
ViewModel.Add(new EmployeeViewModel(employee));
}

return Json(ViewModel, JsonRequestBehavior.AllowGet);
}

我的观点如下:

@model IEnumerable<TrainingKendoUI.Models.TRAINING_EMPLOYEES>

<div>
<h2>Multiselect Test</h2>
<label for="required">Required</label>
@(Html.Kendo().MultiSelect()
.Name("required")
.DataTextField("fullName")
.DataValueField("emplolyeeId")
.Placeholder("Select Employees")
.Filter(FilterType.Contains)
.DataSource(source =>
{
source.Read(read =>
{
read.Action("Employees_Read", "Employee");
})
.ServerFiltering(true);
})
)
<button class="k-button" id="get">Send Invitations</button>
</div>
<script>
$(document).ready(function () {
var required = $("#required").data("kendoMultiSelect");

var items = $("#required").data("kendoMultiSelect");

$("#get").click(function () {
alert("Employees:\n\nIds: " + required.value());
});
});
</script>

非常感谢!

最佳答案

您可以使用dataItems方法:

// get selected dataItems from the control 
var control = $("#required").data("kendoMultiSelect");
var selectedDataItems = control.dataItems();

// create an array that only contains the selected ids
var ids = [];
$(selectedDataItems).each(function() {
ids.push(this.employeeId); // you can access any property on your model here
});

( demo )

关于c# - 从 ASP.Net MVC 4 中的 Kendo UI MultiSelect 获取 PrimaryId,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21781079/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com