向MVC中的视图发送连接结果



我试图发送一个连接结果视图,我在控制器中获得确切的值,但它在视图中接收空值。我的代码有什么问题?这是我的代码

public class ViewModel
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
        public string EmployeeGender { get; set; }
        public string EmployeeCity { get; set; }
        public string EmployeeEmail { get; set; }
        public int DepartmentId { get; set; }
        public string DepartmentName { get; set; }
        public IEnumerable<ViewModel> vm { get; set; }
    }

public ActionResult Details(int id)
        {
            EmployeeContext employeeContext = new EmployeeContext();
            ViewModel vm1 = new ViewModel();
            vm1.vm = from e in employeeContext.Employees
                     join d in employeeContext.Departments on e.DepartmentId equals d.DepartmentId
                     select new ViewModel { EmployeeId = e.EmployeeId, EmployeeName = e.EmployeeName, EmployeeGender = e.EmployeeGender, EmployeeEmail = e.EmployeeEmail, EmployeeCity = e.EmployeeCity, DepartmentName = d.DepartmentName };
            vm1.vm.Single(i => i.EmployeeId == id);
            return View("Details", vm1);
        }
视图

 @model Sample.Models.ViewModel
    <div class="row">
    <div class="col-sm-6 col-md-8">
<h4>
      @Model.EmployeeName
</h4>
    @Model.EmployeeCity
<i class="glyphicon glyphicon-map-marker"></i>                                      
     </div>
     </div>

我想你用错方法了如果只需要Departments表中的部门名称,可以使用下面的代码

EmployeeContext employeeContext = new EmployeeContext();
Employe e = employeeContext.Employees.find(id);
ViewModel vm1 = new ViewModel { EmployeeId = e.EmployeeId, EmployeeName = e.EmployeeName, EmployeeGender = e.EmployeeGender, EmployeeEmail = e.EmployeeEmail, EmployeeCity = e.EmployeeCity, DepartmentName = e.Department.name};
return View("Details", vm1);

相关内容

  • 没有找到相关文章

最新更新