如何在发送一封电子邮件时操作类中的属性列表

本文关键字:操作 列表 属性 电子邮件 | 更新日期: 2023-09-27 18:30:36

我正在尝试在使用购物车的应用程序(MVC 4)中实现订单确认。我正处于想要向客户和管理员用户发送电子邮件以确认订单的阶段。我想为一个订单(购物车)发送一封电子邮件。我的购物车类。

我有一个购物车模型:

  namespace MerchandiseProject.Domain.Entities
 {
      public class Cart
      {
        private List<CartLine> lineCollection = new List<CartLine>();
        public void AddItem(Product product, int quantity) 
        {
        CartLine line = lineCollection
        .Where(p => p.Product.ProductID == product.ProductID)
        .FirstOrDefault();
            if (line == null)
            {
                lineCollection.Add(new CartLine { Product = product,
                Quantity = quantity });
            }
        else 
            {
                line.Quantity += quantity;
            }
       }
        public void RemoveLine(Product product)
        {
            lineCollection.RemoveAll(l => l.Product.ProductID == product.ProductID);
        }
        public decimal ComputeTotalValue()
        {
            return lineCollection.Sum(e => e.Product.Price * e.Quantity);
        }
        public void Clear()
        {
            lineCollection.Clear();
        }
        public IEnumerable<CartLine> Lines 
        {
            get { return lineCollection; }
        }
     }
      public class CartLine
      {
          public Product Product { get; set; }
          public int Quantity { get; set; }
      }
  }

我正在将此模型和运输详细信息模型传递回 POST 结帐操作结果在我的购物车控制器中,并将它们的值分配给 Orders 类,这些详细信息也保留到数据库中。传递回数据库的信息是正确的。但是如何以"foreach"语句之类的方式提取它让我陷入困境!

订单类:

 namespace MerchandiseProject.Domain.Entities
 {
public class Orders
{
    private List<OrderLine> lineCollection = new List<OrderLine>();
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int OrderId { get; set; }
    public DateTime OrderDate { get; set; }
    public int ProductID { get; set; }
    public string ProductName { get; set; }
    public decimal ProductUnitPrice { get; set; }
    public int Quantity { get; set; }
    public decimal OrderTotal { get; set; }
    public string Name { get; set; }
    public string UserName { get; set; }
    public string Line1 { get; set; }
    public string Line2 { get; set; }
    public string Line3 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string Country { get; set; }
    public bool GiftWrap { get; set; }
    public IEnumerable<OrderLine> Lines
    {
        get { return lineCollection; }
    }
}
    public class OrderLine
    {
        public Product Product { get; set; }
        public int Quantity { get; set; }
    }
 }

我的问题是在我的购物车控制器结帐查看结果 [POST] 中,购物车中的每种类型的产品都会收到一封电子邮件,我想发送一封电子邮件,其中包含订购的产品列表? 有意义吗?

购物车控制器:

  [HttpPost]
    public ViewResult Checkout(Cart cart, ShippingDetails shippingDetails)
    {
        var CurrentUser = HttpContext.User;
        string currentUserName = CurrentUser.Identity.Name;
        if (cart.Lines.Count() == 0)
        {
            ModelState.AddModelError("", "Sorry, your cart is empty!");
        }
        if (ModelState.IsValid)
        {
            foreach (var line in cart.Lines)
            {
                var model = new Orders()
                {
                    ProductName = line.Product.Name,
                    ProductUnitPrice = line.Product.Price,
                    Quantity = line.Quantity,
                    OrderTotal = line.Quantity * line.Product.Price,
                    UserName = currentUserName,
                    Name = shippingDetails.Name,
                    Line1 = shippingDetails.Line1,
                    Line2 = shippingDetails.Line2,
                    Line3 = shippingDetails.Line3,
                    City = shippingDetails.City,
                    State = shippingDetails.State,
                    Country = shippingDetails.Country,
                    GiftWrap = shippingDetails.GiftWrap,
                    OrderDate = DateTime.Now,
                    Zip = shippingDetails.Zip,
                };
                new MailController(_actionMailer).OrderDetailsEmail(model).Deliver();
            }
            orderProcessor.SaveOrder(currentUserName ,cart, shippingDetails);
            cart.Clear();
            return View("Completed");
        }
        else
        {
            return View(shippingDetails);
        }
    }

请注意 - 使用剃刀视图 asp.net MVC 4。

如何在发送一封电子邮件时操作类中的属性列表

正如@DStanley所说,您实际上是在说通过将MailController行放在foreach循环中来为每个行项目发送电子邮件。如果问题是您需要访问 foreach 循环上下文之外的model,则需要将其保留在父作用域的变量中。例如:

var lineItems = new List<Order>();
foreach (var line in cart.Lines)
{
    lineItems.Add(new Order
    {
        ...
    });
}

但是,如果您要这样做,则仅使用Select更有效:

var lineItems = cart.Lines.Select(m => new Order
{
    ProductName = m.Product.Name,
    ...
});

完全消除对foreach的需求。