从ViewModel检索数据
本文关键字:数据 检索 ViewModel | 更新日期: 2023-09-27 17:58:19
我现在有两个模型和一个ViewModel。下面列出了它们。我想在索引方法上显示员工模型中的所有字段以及部门模型中的2个字段。因此,我创建了带有所有字段的ViewModel,并在我的Create Method上添加了一个用于检索数据的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],但此ViewDataDiction实例需要类型为"System.Collections.Generic.IEnumerable`1[DDataEntryMVC.Models.EmpireeViewModel]"的模型项。
此外,我是否正确处理了将数据添加到数据库的创建方法?
这个错误不言自明。视图被强类型化为EmployeeViewModel
类(IEnumerable<EmployeeViewModel>
)的集合,但在操作方法中,返回的是Employee
实体的列表。
solation是将实体列表转换为视图模型列表,并将其返回到视图。您可以使用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);
}