我现在有2个模型和1个ViewModel。它们列在下面。我想显示我的员工模型中的所有字段以及索引方法中我的部门模型中的2个字段。因此,我创建了包含所有字段的ViewModel,并添加了public IEnumerable<SelectListItem> DeptList { get; set; }
以便在我的创建方法上的DropDownList中检索数据(工作正常)。
员工模型:
public class Employee
{
[Key]
public int EmpId { get; set; }
[Column(TypeName = "varchar(50)")]
[MaxLength(50)]
[Required(ErrorMessage = "First Name is Required")]
public string EmpFirstName { get; set; }
[Column(TypeName = "varchar(50)")]
[MaxLength(50)]
[Required(ErrorMessage = "Last Name is required")]
public string EmpLastName { get; set; }
[Column(TypeName = "varchar(10)")]
[DataType(DataType.PhoneNumber)]
[MaxLength(10)]
[Required(ErrorMessage = "Phone Number is required")]
public string EmpPhoneNumber { get; set; }
[Column(TypeName = "datetime2(7)")]
[DataType(DataType.Date)]
[Required(ErrorMessage = "Start Date is required")]
[DisplayFormat(DataFormatString = "{0:D}")]
public DateTime EmpStartDate { get; set; }
public int DeptId { get; set; }
public Department Department { get; set; }
}
部门型号:
public class Department
{
[Key]
public int DeptId { get; set; }
[Required(ErrorMessage = "Department Name is required")]
[Column(TypeName = "varchar(50)")]
[MaxLength(50)]
public string DeptName { get; set; }
public List<Employee> Employees { get; set; }
}
EmployeeViewModel:
如果可能的话,我想为我的CRUD创建一个ViewModel。
public class EmployeeViewModel
{
public int EmpId { get; set; }
public string EmpFirstName { get; set; }
public string EmpLastName { get; set; }
public string EmpPhoneNumber { get; set; }
public DateTime EmpStartDate { get; set; }
public int DeptId { get; set; }
public string DeptName { get; set; }
public IEnumerable<SelectListItem> DeptList { get; set; }
}
员工控制人:
public class EmployeeController : Controller
{
private DataEntryContext _context;
public EmployeeController(DataEntryContext context)
{
_context = context;
}
// GET: /<controller>/
public IActionResult Index()
{
return View(_context.Employees.ToList());
}
// Retrieve List of Departments
private IEnumerable<SelectListItem> GetDeptList()
{
var dept = _context.Departments
.Select(s => new SelectListItem
{
Value = s.DeptId.ToString(),
Text = s.DeptName
})
.ToList();
return (dept);
}
public IActionResult Create()
{
EmployeeViewModel selectList = new EmployeeViewModel();
selectList.DeptList = GetDeptList();
return View(selectList);
}
[HttpPost]
[ValidateAntiForgeryToken]
public IActionResult Create(EmployeeViewModel employee)
{
if (ModelState.IsValid)
{
var emp = new Employee();
{
emp.EmpFirstName = employee.EmpFirstName;
emp.EmpLastName = employee.EmpLastName;
emp.EmpPhoneNumber = employee.EmpPhoneNumber;
emp.EmpStartDate = employee.EmpStartDate;
emp.DeptId = employee.DeptId;
}
_context.Employees.Add(emp);
_context.SaveChanges();
return RedirectToAction("Index");
}
return View(employee);
}
}
查看索引方法:
我在此视图的顶部添加了@model IEnumerable<DataEntryMVC.Models.EmployeeViewModel>
。
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.EmpFirstName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpLastName)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpPhoneNumber)
</td>
<td>
@Html.DisplayFor(modelItem => item.EmpStartDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.DeptName)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.EmpId })
</td>
</tr>
}
检索数据时收到的错误消息:
处理请求时发生未处理的异常。
InvalidOperationException:传递给ViewDataDictionary的模型项的类型为System.Collections.Generic.List`1 [DataEntryMVC.Models.Employee],但此ViewDataDictionary实例需要类型为“System.Collections.Generic.IEnumerable”的模型项。 `1 [DataEntryMVC.Models.EmployeeViewModel]”。
另外,我是否正确处理了将数据添加到数据库的创建方法?
答案 0 :(得分:0)
错误是不言自明的。您的视图是针对EmployeeViewModel
类(IEnumerable<EmployeeViewModel>
)的集合强类型的,但在您的操作方法中,您将返回Employee
个实体的列表。
solition是将实体列表转换为视图模型列表并将其返回到视图。您可以使用LINQ Select
方法执行此操作。
public IActionResult Index()
{
var list=_context.Employees
.Select(s=> new EmployeeViewModel {
EmpId =s.EmpId ,
EmpFirstName=s.EmpFirstName,
EmpLastName=s.EmpLastName,
EmpPhoneNumber=s.EmpPhoneNumber,
EmpStartDate=s.EmpStartDate,
DeptName=s.Department.DeptName
}).ToList();
return View(list);
}