在ASP.NETMVC中将预订与航班关联

o7jaxewo  于 2023-04-08  发布在  .NET
关注(0)|答案(1)|浏览(161)

我正在创建一个航班预订网站使用ASP.NET MVC。有很多航班,我想添加的功能,基本上是使某个航班的机票预订。
我有以下型号:

public class Flight
{
    [Key]
    public int Id { get; set; }

    public string LocationFrom { get; set; }
    public string LocationTo { get; set; }
    public DateTime DepartureDate { get; set; }
    public DateTime ArrivalDate { get; set; }
    public PlaneType PlaneType { get; set; }
    public string PlaneId { get; set; }
    public string PilotName { get; set; }
    public int AvailableSeats { get; set; }
    public int AvailableSeatsBusinessClass { get; set; }

    public virtual HashSet<Reservation> Reservations { get; set; }

    public Flight()
    {
        Reservations = new HashSet<Reservation>();
    }
}

public class Reservation
{
    [Key]
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public string LastName { get; set; }
    public string PersonalID { get; set; }
    public string PhoneNumber { get; set; }
    public string Nationality { get; set; }
    public TicketType TicketType { get; set; }
    public string Email { get; set; }

    public virtual HashSet<Flight> Flights { get; set; }

    public Reservation()
    {
        Flights = new HashSet<Flight>();
    }
}

以下是我在预订页面中使用的创建预订方法:

// GET: Reservations/Create
public async Task<IActionResult> Create()
{
    return View();
}

// POST: Reservations/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("Id,FirstName,SecondName,LastName,PersonalID,PhoneNumber,Nationality,TicketType,Email")] Reservation reservation)
{
    if (ModelState.IsValid)
    {
        _context.Add(reservation);
        await _context.SaveChangesAsync();
        return RedirectToAction(nameof(Index));
    }

    return View(reservation);
}

由于我有一个单独的预订页面,我想将预订链接到某个航班,所以当我点击航班页面上的详细信息按钮时,我可以看到该航班的所有预订,但我不知道如何做到这一点。
你能给我一些建议吗?顺便说一句,我在我的项目中使用SQL Server作为本地数据库和实体框架。

vcudknz3

vcudknz31#

表的设计应该是这样的。EF将自动创建关系。

public class Flight
{
    [Key]
    public int Id { get; set; }

    public string LocationFrom { get; set; }
    public string LocationTo { get; set; }
    public DateTime DepartureDate { get; set; }
    public DateTime ArrivalDate { get; set; }
    public PlaneType PlaneType { get; set; }
    public string PlaneId { get; set; }
    public string PilotName { get; set; }
    public int AvailableSeats { get; set; }
    public int AvailableSeatsBusinessClass { get; set; }

    public virtual ICollection<Reservations> Reservations { get; set; }
}

 public class Reservation
   {
   {
    [Key]
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string SecondName { get; set; }
    public string LastName { get; set; }
    public string PersonalID { get; set; }
    public string PhoneNumber { get; set; }
    public string Nationality { get; set; }
    public TicketType TicketType { get; set; }
    public string Email { get; set; }
    public int FlightId { get; set; }
    [NotMapped]
    public string FlightNo {get; set;}
   
    public virtual Flight Flight { get; set; }
   
}

注意,NotMapped数据注解是在视图中显示航班号,而不需要将字段保存在数据库中。控制器将显示如何显示它。
对于控制器
我想你应该先创建一个ActionResult来搜索航班。然后让用户选择航班并重定向到创建预订航班详细信息的操作。

public async Task<IActionResult> Create(int FlightId)
{
    var model = new Reservation();
    Reservation.FlightId = FlightId;
    Reservation.FlightNo = db.Flights.Find(FlightId).Code;
    return View(model);
}

Post方法将是相同的,除了你需要在FlightId中添加。

相关问题