在单个控制器中使用多个模型
我已经使用数据库中的EF Designer从为我的项目创建的数据库中构建我的模型。 对于单个控制器,我需要引用多个模型。 这方面的一个例子是有一个Users表,其中包含用户部门和办公室。 我需要引用两个单独的表DepartmentPermissions和OfficePermissions来确定用户能够看到的数据。
以下是自动生成模型的示例:
//------------------------------------------------------------------------------ // // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // //------------------------------------------------------------------------------ namespace Project.Models { using System; using System.Collections.Generic; public partial class User { public int User_ID { get; set; } public string User_Username { get; set; } public string User_Department { get; set; } public string User_Office { get; set; } } } //------------------------------------------------------------------------------ // // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // //------------------------------------------------------------------------------ namespace Project.Models { using System; using System.Collections.Generic; public partial class DepartmentPermission { public int DeptPerm_ID { get; set; } public string DeptPerm_Department { get; set; } public string DeptPerm_Role { get; set; } } } //------------------------------------------------------------------------------ // // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // //------------------------------------------------------------------------------ namespace Project.Models { using System; using System.Collections.Generic; public partial class OfficePermission { public string OffPerm_OfficeID { get; set; } } }
每个表中的第一列是数据库中的主键。
使用任何这些自动生成的模型创建控制器时,一切正常,除了我只能使用一个表。
我希望能够从Users表中检查用户的部门和办公室,然后从DepartmentPermissions表中检查与用户部门关联的角色,然后检查用户的办公室是否在OfficePermissions表中。
我读到我可以构建一个View Model来创建一个新模型,它结合了我想要使用的模型,这样我就可以从一个控制器中的所有相关表中获取信息。
我的示例ViewModel是:
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Project.Models { public class HomeViewModel { public IEnumerable Users { get; set; } public IEnumerable OfficePermissions { get; set; } public IEnumerable DepartmentPermissions { get; set; } } }
当我尝试使用此视图模型创建Controller时,出现错误:
User :: EntityType’User’没有定义键。 定义此实体类型的键。 OfficePermission :: EntityType’OfficePermission’没有定义键。 定义此实体类型的键。 DepartmentPermission :: EntityType’DepartmentPermission’没有定义键。 定义此实体类型的键。 HomeViewModel :: EntityType’HomeViewModel’没有定义键。 定义此实体类型的键。 用户:EntityType:EntitySet’Users’基于未定义键的’User’类型。 OfficePermissions:EntityType:EntitySet’OfficePermissions’基于类型’OfficePermission’,没有定义键。 DepartmentPermissions:EntityType:EntitySet’DepartmentPermissions’基于类型’DepartmentPermission’,没有定义键。 HomeViewModels:EntityType:EntitySet’HomeViewModels’基于类型’HomeViewModel’,没有定义键。
我可以在自动生成的文件中手动定义密钥,但是当更改数据库并且文件需要自动重新生成时,这将被取消。
有没有办法组合这些自动生成的模型,以便在控制器中一起使用它们?
你有不同的选择,你可以使用其中任何一个
使用ViewModel
对于视图模型,您必须创建一个类,在此类中,您将所有模型定义为此类的属性。这里有两个类。
public class EmployeeDetails { [Required] [Display(Name = "Name")] public string Name { get; set; } } public class Employee { public int Id { get; set; } }
这是viewmodel
public class ViewModel { public Employee emp { get; set; } public EmployeeDetails empdet{ get; set; } }
现在在Controller你会这样做
public ActionResult About() { ViewModel vm = new ViewModel(); vm.emp = new Employee(); vm.empdet = new EmployeeDetails(); return View(vm); }
在视图中你会收到这样的
@model ViewModel
正如您所说,您正在获取错误表没有定义键,您应该正确定义表的键。
使用元组
元组用于存储不同的类型。您可以将所需的类对象存储在其中并传递给视图
在控制器中
Tuple tuple = new Tuple(1, "Hello world"); return View(tuple);
在视图中,您将收到这样的信息
@model Tuple