代码之家  ›  专栏  ›  技术社区  ›  Stian

将“所有者”标识传递给相关模型的创建视图

  •  0
  • Stian  · 技术社区  · 6 年前

    我有这个视图模型 Employees :

    public class EmployeeViewModel
    {
        public int Id { get; set; }
        // some more properties
        public List<EmployeeReservationViewModel> Reservations { get; set; }
    }
    

    …这个是为了 EmployeeReservations :

    public class EmployeeReservationViewModel
    {
        public int Id { get; set; }
        public int EmployeeId { get; set; }
        // some more properties
        public EmployeeViewModel Employee { get; set; }
    }
    

    员工 Details -视图,我有一个用于添加新 EmployeeReservation ,像这样:

    <a asp-controller="EmployeeReservations" 
       asp-action="Create">Add reservation</a>
    

    我该怎么通过 EmployeeId EmployeeReservations/Create -查看?

    目前,我在 员工 控制器中的详细方法:

    HttpContext.Session.SetInt32("EmployeeId", employee.Id);
    

    …然后在 雇员保留 -控制器:

    employeeReservation.EmployeeId = HttpContext.Session.GetInt32("EmployeeId").Value;
    db.Add(employeeReservation);
    await db.SaveChangesAsync();
    

    我觉得我的方法不是最佳的。如果会话超时怎么办?还有其他更安全的方法吗?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Jeswin Rebil    6 年前

    可以使用ASP路由ID

    在视野中

    <a asp-controller="EmployeeReservations" 
       asp-action="Create" asp-route-id="10">Add reservation</a>
    

    在控制器中,在创建操作之前添加ID作为参数

    Public IActionResult Create(string id){
    }