如何获取外键代表MVC4的值

时间:2015-04-16 18:49:19

标签: database asp.net-mvc-4 view controller

目前,我的模型代表了我数据库中的一个表。

public partial class Booking
{
    public int BookingId { get; set; }
    public string BookingFirstname { get; set; }
    public string BookingSurname { get; set; }
    public string BookingEmail { get; set; }
    public string BookingMobileTel { get; set; }
    public string BookingHomeTel { get; set; }
    public int BookingAge { get; set; }
    public int BookingPassengers { get; set; }
    public int DepartureId { get; set; }
    public int ArrivalId { get; set; }
}

DepartureId和ArrivalId是另外两个表的外键。

public partial class Departure
{
    public int DepartureId { get; set; }
    public string DepartureName { get; set; }
}

public partial class Arrival
{
    public int ArrivalId { get; set; }
    public int DepartureId { get; set; }
    public string ArrivalName { get; set; }
}

我希望能够在用户从图书视图提交表单时从所选的选项中获取到达名称和出发名称。

这是[HttpPost]图书行动结果:

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Book(Booking bk)
    {
        List<Departure> departures = new List<Departure>();
        List<Arrival> arrivals = new List<Arrival>();

        using (BookingEntities db = new BookingEntities())
        {
            departures = db.Departures.OrderBy(a => a.DepartureName).ToList();
            if (bk != null && bk.DepartureId > 0)
            {
                arrivals = db.Arrivals.Where(a => a.DepartureId.Equals(bk.DepartureId)).OrderBy(a => a.ArrivalName).ToList();
            }
        }

        ViewBag.DepartureId = new SelectList(departures, "DepartureId", "DepartureName", bk.DepartureId);
        ViewBag.ArrivalId = new SelectList(arrivals, "ArrivalId", "ArrivalName", bk.ArrivalId);

        string emailTo = bk.BookingEmail;
        string emailBody = "Thank you for Booking with Callum Airways, " + bk.BookingFirstname + " " + bk.BookingSurname + ".\n" +
            "Here is confirmation that your booking was a success. We have listed the information you entered into this email.\n" +
            "Email: " + bk.BookingEmail + ",\n" +
            "Mobile Tel: " + bk.BookingMobileTel + ",\n" +
            "Home Tel: " + bk.BookingHomeTel + ",\n" +
            "Age: " + bk.BookingAge + ",\n" +
            "Number of Passengers: " + bk.BookingPassengers + ".\n\n" +
            // Departure and Arrival Names
            "If you want to review/delete your booking please register an account on ************.";


        // All the other booking information.

        using (BookingEntities db = new BookingEntities())
        {
            if (ModelState.IsValid)
            {
                db.Bookings.Add(bk);
                db.SaveChanges();
                ModelState.Clear();
                bk = null;                    

                MailAddress from = new MailAddress("*************@gmail.com");
                MailAddress to = new MailAddress(emailTo);
                MailMessage message = new MailMessage(from, to);
                message.Subject = "Booking Confirmation - ********";
                message.Body = emailBody;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.Port = 587;
                smtp.UseDefaultCredentials = false;

                smtp.Timeout = 10000;
                smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                smtp.Credentials = new System.Net.NetworkCredential("*********************", "*******");
                smtp.EnableSsl = true;
                smtp.Send(message);
            }              
        }
        return View("~/Views/Home/Index.cshtml", bk);
    }

我已经能够获得BookingFirstname和其他人的价值,但因为只有外键而不是预订舱中的名字。我不确定如何获得ArrivalName和DepartureName。

1 个答案:

答案 0 :(得分:1)

Arrival arrival = new Arrival();
arrival = db.Arrival.Where(w => w.ArrivalId.Equals(bk.ArrivalId).FirstOrDefault();
ViewBag.ArrivalName = arrival.ArrivalName; 

Departure departure = new Departure();
departure = db.Departure.Where(w => w.DeaId.Equals(bk.ArrivalId).FirstOrDefault();
ViewBag.DepartureName = departure.DepartureName;