我有一个由以下模型表示的旧表:
public class Employee
{
[Key]
[Column("employee_id")]
public string EmployeeId { get; set; }
[Column("first_name")]
public string FirstName { get; set; }
[Column("last_name")]
public string LastName { get; set; }
[Column("active")]
public bool Active { get; set; }
}
我没有此类的视图模型,它只会用于在我的应用程序的其他视图模型中填充 SelectLists(通过存储库)。但是,我想创建一个像这样的属性,以连接 SelectLists/下拉列表的名字和姓氏:
private string _EmployeeName;
public string EmployeeName
{
get
{
return _EmployeeName;
}
set
{
_EmployeeName = this.FirstName + " " + this.LastName;
}
}
当我将 EmployeeName 放入我的 Employee 模型时,我收到一个错误,即 EmployeeName 列不存在。好的,有道理,因为这是我的模型,他们没有这样的专栏。
下面是一个使用 SelectList 的视图模型的简短示例:
public class EquipmentViewModel
{
[Display(Name = "Equipment ID:")]
public string EquipmentId { get; set; }
[Required(ErrorMessage = "Equipment name is required.")]
[Display(Name = "Equipment Name:")]
[MaxLength(128)]
public string EquipmentName { get; set; }
public SelectList EmployeeList { get; set; }
}
在我的控制器中,我这样做:
var emp = iEmployeeRepository.FindBy(x => x.Active == true).OrderBy(x => x.FirstName);
var equipmentViewModel = new EquipmentViewModel
{
EquipmentId = e.EquipmentId,
EquipmentName = e.EquipmentName,
OriginatorEmployeeId = e.OriginatorEmployeeId,
EmployeeList = new SelectList(emp, "EmployeeId", "FirstName"),
};
return View(equipmentViewModel);
由于我没有 Employee 类的视图模型,我可以将这个 EmployeeName 属性放在哪里来替换 FirstName?如果有人能指出我正确的方向,我将不胜感激。