0
我剛剛開始使用ASP.Net和Kendo,並且非常感謝您可以給予的任何幫助。我正在嘗試使用Kendo MultiSelect Widget在我的控制器上使用Json方法來顯示Employees(我已成功管理)的列表,以從我的視圖模型返回結果集。我希望能夠實現的是收集employeeId列表,以便我可以將它們發送回控制器進行處理。但是,我似乎只能得到列表中的值而不是Id。從ASP.Net MVC中的Kendo UI MultiSelect獲取PrimaryId 4
有人可以給我一些關於如何實現這一目標的建議嗎?
我的視圖模型是這樣的:
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>
非常感謝!
非常感謝的是,它的偉大工程!我也很喜歡下面的代碼,儘管它不如你的優雅! var ids = []; for(var i = 0,l = required._dataItems.length; i <1; i ++){ids.push(required._dataItems [i] .employeeId); } – user2883382