How to join two tables in action result and bind with kendo grid
嗨,我将剑道网格与下面的两个表绑定在一起,这是我为此使用的顺序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class EFEmployeeRepositary : IEmployeeRepositary { private EFDBContext context; public EFEmployeeRepositary() { this.context = new EFDBContext(); } public IQueryable<Entities.Employee> Employees { get { return context.Employees; } } public IQueryable<Entities.GetEmployeeDetails> GetEmployeesDetails { get { return context.GetEmployeesDetails; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | [System.ComponentModel.DataAnnotations.Schema.Table("Table1", Schema ="dbo")] public class Employee { [System.ComponentModel.DataAnnotations.Key] public int Staff_Seq_Number { get; set; } public string Staff_Number { get; set; } public string Full_Name { get; set; } public DateTime DOJ { get; set; } public DateTime DOB { get; set; } public string Email_Address { get; set; } public string Nationality_Code { get; set; } public int Age { get; set; } public string Marital_Status_Code { get; set; } public string Gender_Code { get; set; } } [System.ComponentModel.DataAnnotations.Schema.Table("Table2", Schema ="dbo")] public class GetEmployeeDetails { [System.ComponentModel.DataAnnotations.Key] public int Staff_Seq_Number { get; set; } public int Matrix_ID { get; set; } public int Grade_ID { get; set; } public int Position_ID { get; set; } } public class MergeTables { public Employee employees { get; set; } public GetEmployeeDetails GetEmployeesDetails { get; set; } } |
这是我的Interface EmployeeRepository类,在其中我同时添加了两个类
1 2 3 4 5 6 | public interface IEmployeeRepositary { IQueryable<Employee> Employees { get; } IQueryable<GetEmployeeDetails> GetEmployeesDetails { get; } IQueryable<MergeTables> GetMergeTables { get; } } |
这是我的dbcontext类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public DbSet<Employee> Employees { get; set; } public DbSet<EmployeeHistory> EmployeesHistory { get; set; } public DbSet<MergeTables> GetMergeTables { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //base.OnModelCreating(modelBuilder); modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>(); } |
这是我的控制器,称为此接口存储库
1 2 3 4 5 6 7 8 | private IEmployeeRepositary employeeRepositary; public DashboardController(IEmployeeRepositary pEmployeeRepositary) { this.employeeRepositary = pEmployeeRepositary; } |
在同一控制器中,我具有此actionresult函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public ActionResult Employee_Read([DataSourceRequest] DataSourceRequest request) { var p = (from emp in this.employeeRepositary.Employees join empdetails in this.employeeRepositary.GetEmployeesDetails on emp.Staff_Seq_Number equals empdetails.Staff_Seq_Number select new MergeTables() { }); return Json(p, JsonRequestBehavior.AllowGet); } |
第一个问题:运行此程序时,我收到以下错误消息:"在模型生成过程中检测到一个或多个验证错误,但未定义键",我在那创建的新类MergeTables出现问题。
第二个问题:如果我从存储库和DBContext类中删除合并表,则此查询工作正常,但剑道网格中未显示任何数据
我在这里显示了两个模型,但是我使用了两个以上的模型并将它们绑定到Action结果中。
下面是剑道网格代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | @(Html.Kendo().Grid<GEMS.Domain.Entities.MergeTables>() .Name("grid") .Columns(columns => { columns.Bound(p => p.employees.Staff_Number).Title("EmployeeId"); columns.Bound(p => p.employees.Full_Name).Title("Name"); columns.Bound(p => p.employees.Age).Title("Age"); columns.Bound(p => p.employees.Email_Address).Title("EmailId"); columns.Bound(p => p.GetHierarchyTemp.Department).Title("Department"); columns.Bound(p => p.employees.DOB).Title("Date of birth"); columns.Bound(p => p.employees.DOJ).Title("Date of joining"); columns.Bound(p => p.employees.Marital_Status_Code).Title("Marital Status"); columns.Bound(p => p.employees.Nationality_Code).Title("Nationality Code"); }) .Scrollable() .Groupable() .Sortable() .ColumnMenu() .Filterable() .ClientDetailTemplateId("template") .Resizable(resize => resize.Columns(true)) .Reorderable(reorder => reorder.Columns(true)) .Selectable(selectable => selectable .Mode(GridSelectionMode.Multiple) .Type(GridSelectionType.Cell)) .Navigatable() .AllowCopy(true) .Pageable(pageable => pageable .Refresh(true) .PageSizes(true) .ButtonCount(10)) .HtmlAttributes(new { style ="height:380px;" }) .DataSource(dataSource => dataSource .Ajax() .Read(read => read.Action("Employee_Read","Dashboard")) ) .Events(events => events.DataBound("dataBound")) ) |
谁能帮我解决这两个问题?
1 2 3 4 5 6 7 8 9 10 11 12 | use the word [Key] above your model items. example: class telefoon{ [Key] Int id {get:set} int number{get:set} } like this |