我想用一个员工列表填充一个kendo-grid。当我将员工列表直接发送到网格时,网格会填充。当我稍微操纵列表时,网格是空的。员工类有一个我需要操作的bool“selected”字段。在这两种情况下,当在调试器中停止控制器时,列表中有600名员工,因此数据就在那里。
以下是视图中的代码(我甚至没有绑定到“selected”属性 - 认为这可能是问题)
@(Html.Kendo().Grid<TRS.MVC.Models.SelectEmployeeModel>()
.Name("gridEmployee")
.HtmlAttributes(new { style = "height: 300px" })
.Columns(columns =>
{
columns.Bound(p => p.CompanyCode).Width(100);
columns.Bound(p => p.EmployeeCode).Width(100);
columns.Bound(p => p.EmployeeName).Width(100);
})
.Selectable(selectable => selectable
.Mode(GridSelectionMode.Single))
.Navigatable()
.Scrollable()
.DataSource(dataSource => dataSource
.Ajax()
.Read(read => read.Action("GetEmployees", "EmployeeSelectDlg"))
)
)
以下是有效的代码
public ActionResult GetEmployees([DataSourceRequest]DataSourceRequest request)
{
List<SelectEmployeeModel> employeesForGrid;
employeesForGrid = (from e in trs.Employees select e).ToList();
return Json(employeesForGrid.ToDataSourceResult(request));
}
不起作用的代码(网格为空)。在两种情况下,当在调试器中停止时,“employeesForGrid”有600个项目。
public ActionResult GetEmployees([DataSourceRequest]DataSourceRequest request)
{
List<Employee> jobEmployees;
List<SelectEmployeeModel> employeesForGrid;
Job selectedJob = (from j in trs.Jobs.Include("JobEmployees") where j.JobKey == selectedJobKey select j).First();
if (selectedJob.JobEmployees.Any())
jobEmployees = (from j in selectedJob.JobEmployees select j).Select(eq => eq.Employee).ToList();
else
jobEmployees = new List<Employee>();
employeesForGrid = (from e in trs.Employees select new SelectEmployeeModel() { Employee = e, Selected = jobEmployees.Contains(e) }).ToList();
return Json(employeesForGrid.ToDataSourceResult(request));
}
没有错误消息只是一个空网格。
答案 0 :(得分:0)
对我而言,结构是不同的,在第一种情况下,集合中包含SelectEmployeeModel类型的对象。
employeesForGrid = (from e in trs.Employees select e).ToList();
在第二种情况下,集合中包含SelectEmployeeModels对象,其中Employee属性实际上是SelectEmployeeModel,其余属性似乎未指定。
如果将列绑定到嵌套属性
,会发生什么 columns.Bound(p => p.Employee.CompanyCode).Width(100);
columns.Bound(p => p.Employee.EmployeeCode).Width(100);
columns.Bound(p => p.Employee.EmployeeName).Width(100);
答案 1 :(得分:0)
事实证明你不能这样做:
public class SelectEmployeeModel
{
public Employee Employee { get; set; }
public bool Selected { get; set; }
public string CompanyCode { get { return this.Employee.CompanyCode; } }
public string EmployeeCode { get { return this.Employee.EmployeeCode; } }
public string EmployeeName { get { return this.Employee.EmployeeName; } }
public SelectEmployeeModel()
{
}
}
相反,我只是这样做了:
public class SelectEmployeeModel
{
public bool Selected { get; set; }
public string CompanyCode { get; set; }
public string EmployeeCode { get; set; }
public string EmployeeName { get; set; }
public SelectEmployeeModel()
{
}
}
并更改了控制器代码:
employeesForGrid = (from e in trs.Employees select new SelectEmployeeModel() {
CompanyCode = e.CompanyCode,
EmployeeCode = e.EmployeeCode,
EmployeeName = e.EmployeeName,
Selected = jobEmployees.Contains(e) }).ToList();
修正了它。