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

时间:2014-11-17 16:01:50

标签: c# asp.net-mvc-4 razor

我正在尝试在使用购物车的应用程序(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; }
      }
  }

我将此模型和发货详细信息模型传递回我的Cartcontroller中的POST checkout actionResult并将其值分配给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; }
    }
 }

我的问题出在我的购物车控制器Checkout ViewResult [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.

1 个答案:

答案 0 :(得分:0)

正如@DStanley所说,您实际上是要通过在foreach循环中使用MailController行来为每个订单项发送电子邮件。如果问题是您需要在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