Reputation: 25
Currently, I have model which represents a table within my database.
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 and ArrivalId are foreign keys of two other tables.
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; }
}
I want to be able to get the Arrival Name and Departure Name from the option selected when the user submits a form from the Book View.
This is currently the [HttpPost] Book Action Result:
[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);
}
I've been able to get the value of BookingFirstname and others but because there is just the foreign key not the name in the Booking Class. I'm not sure how I can get ArrivalName and DepartureName.
Upvotes: 1
Views: 54
Reputation: 514
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;
Upvotes: 1