我正在尝试找到一个解决方案,了解如何将电子邮件发送到多个Hospital 电子邮件地址,具体取决于哪些订单与正在进行更改的 Delivery 相关联 - 使用Postal。
因此,对于一些上下文,我希望将相同的电子邮件发送给所有具有与交付相关的订单的医院。因此,在编辑交付时(例如更改status
),必须向在该交付中有订单的所有医院发送一封电子邮件。
问题是交付可以有许多订单,而不一定只有一个,因此,我不确定如何定位在该特定交付中拥有订单的所有医院的所有电子邮件地址。
此外,医院仅与订单相关联,而订单与交货相关联 - 访问医院电子邮件的唯一方法是从Hospital
表格中。这意味着我必须穿过Order
桌子然后到达Hospital
桌子。
我该怎么做呢?我对 MVC 还是很陌生,所以我会很感激我能得到的所有帮助。
我在Delivery Controller中的编辑方法的POST中执行此操作,因为我只希望在进行编辑后从那里发送电子邮件。因此,为什么我在. savechanges()
这些台词只是我的尝试Order order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault();
。Hospital hospital = db.Hospitals.(h => h.HospitalID == order.HospitalID);
它们不起作用,并且不是原始代码的一部分。
如果需要任何其他代码,请告诉我,我会将其添加到问题中!
楷模
交货
public class Delivery
{
public int DeliveryID { get; set; }
public int DriverID { get; set; }
public virtual Driver Driver { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
医院
public class Hospital
{
public int HospitalID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
命令
public class Order
{
public int OrderID { get; set; }
public int HospitalID { get; set; }
public int? DeliveryID { get; set; }}
public virtual Hospital Hospital { get; set; }
public virtual Delivery Delivery { get; set; }
}
DeliveryVM viewModel
public class DeliveryVM
{
public int? ID { get; set; }
public int DriverID { get; set; }
public SelectList DriverList { get; set; }
public List<OrderVM> Orders { get; set; }
}
Delivery Controller POST 方法:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(DeliveryVM model)
{
// Get the data model based on the ID of the view model
Delivery delivery = db.Deliverys.Find(model.ID);
// Map the view model properties to the data model
delivery.DriverID = model.DriverID;
....
db.SaveChanges();
//Email
Order order = db.Orders.Where(o => o.OrderID == order.DeliveryID).FirstOrDefault(); // I tried this but it didn't work. It was just an attempt
Hospital hospital = db.Hospitals.(h => h.HospitalID == order.HospitalID); // I was going to use this to retrieve the hospitalIDs linked to the Order? Don't know if thats correct
dynamic email = new Email("Example");
email.ID = delivery.DeliveryID;
email.To = hospital.Email; // this is where you set the email you are sending to.
email.Send();
//End
return RedirectToAction("Index");
}
笔记
- 为了便于阅读,我从 Delivery Edit Method 中删除了很多代码,因此如有必要,我也可以将其添加到问题中。