如何在C#&;中从视图到模型访问数据;ASP.NETMVC

如何在C#&;中从视图到模型访问数据;ASP.NETMVC,c#,asp.net-mvc,C#,Asp.net Mvc,我有类User和Role,我想从Role访问用户名 我的课程是: public class ShiftCalendar { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int ShiftId { get; set; } public virtual Shift Shift { get; set; } public Date

我有类
User
Role
,我想从
Role
访问用户名

我的课程是:

public class ShiftCalendar
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public int ShiftId { get; set; }
    public virtual Shift Shift { get; set; }

    public DateTime Date { get; set; }
    public DateTime ShiftEndTime { get; set; }        

    public User ShiftManager { get; set; }
    public string ShiftManagerId { get; set; }
}

public class Shift
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    [MaxLength(50)]
    public string Description { get; set; }       

    public TimeSpan ShiftBeginTime { get; set; }
    public TimeSpan ShiftEndTime { get; set; }

    public ICollection<ShiftCalendar> ShiftCalendars { get; set; }
}

public class User: IdentityUser
{
    public String FirstName { get; set; }
    public string Surname { get; set; }

    public ICollection<ShiftCalendar> ShiftCalendars { get; set; }
}
将显示其他值(Shift.Description、日期和结束日期)

索引控制器:

public async Task<ActionResult> Index()
{
    var shiftCalendars = _unitOfWork.ShiftCalendarsRepository.Get();
    return View(await shiftCalendars.ToListAsync());
}
公共异步任务索引()
{
var shiftCalendars=\u unitOfWork.ShiftCalendarsRepository.Get();
返回视图(等待shiftCalendars.ToListAsync());
}
我做错了什么


谢谢

EF还是EF核心?选择
ShiftCalendar
时,请尝试在查询中使用
.Include(p=>p.ShiftManager)
,我已使用公共虚拟用户而不是公共用户解决问题
public async Task<ActionResult> Index()
{
    var shiftCalendars = _unitOfWork.ShiftCalendarsRepository.Get();
    return View(await shiftCalendars.ToListAsync());
}