0

我想用员工列表填充剑道网格。当我将员工列表直接发送到网格时,网格会填充。当我稍微操纵列表时,网格是空的。员工类有一个我需要操作的布尔“选定”字段。在这两种情况下,在调试器中停止控制器时,列表都有 600 名员工,所以数据就在那里。

这是视图中的代码(我什至没有绑定到“选定”属性 - 认为这可能是问题所在)

@(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));
    }

没有错误消息只是一个空的网格。

4

2 回答 2

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);
于 2013-05-07T20:15:52.953 回答
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();

那解决了它。

于 2013-05-08T00:15:29.187 回答