gpt4 book ai didi

database - MVC4如何获取外键代表的值

转载 作者:搜寻专家 更新时间:2023-10-30 23:45:13 24 4
gpt4 key购买 nike

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

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; }
}

我希望能够从用户从书本 View 提交表单时选择的选项中获取到达姓名和出发姓名。

这是当前的 [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 和其他值,但因为只有外键而不是 Booking 类中的名称。我不确定如何获得 ArrivalName 和 DepartureName。

最佳答案

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;

关于database - MVC4如何获取外键代表的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683486/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com