MVC3购物车订购

时间:2012-04-16 12:17:30

标签: asp.net-mvc-3

我正在尝试从购物车创建订单。

我想将购物车项目传递到订单表。

目前,我正在尝试将这些列表传递给控制器​​。

如何将购物车中的这些列表传递给AccountController中的Order方法?

哪一个更好的方法(ActionLink或按钮)来传递列表?

的ViewModels:

ShoppingCartViewModel.cs
public class ShoppingCartViewModel
{
    public List<Cart> CartItems { get; set; }
    public int recordId { get; set; }
    public decimal CartTotal { get; set; }
}

这是购物车视图。

index.cshtml in ShoppingCart

@model MvcApplication2.ViewModels.ShoppingCartViewModel

@using (Html.BeginForm("AddtoOrder", "Account", Model.CartItems.ToList()))
{
    <input type="submit" class="button2" value="Checkout" />
}

这是AccountController中的Order方法。

[HttpPost]
    public ActionResult AddtoOrder(Order order)
    {
        //I want to add these values in Order table when I click Checkout button
        //order.customerId = "";
        //order.recordId = 0;
        //order.rentalPeriod = 3;

        db.Order.Add(order);
        db.SaveChanges();

        return View(order);
    }

这是模特;购物车和订单。

public class Cart
{
    [Key]
    public int recordId { get; set; }
    public string cartId { get; set; }
    public int productId { get; set; }
    public decimal priceValue { get; set; }
    public int count { get; set; }
    public int rentalPeriod { get; set; }
    public DateTime dateCreated { get; set; }
    public virtual Product Product { get; set; }
}

public class Order
{
    rentalDB db = new rentalDB();

    [Key] public int orderId { get; set; }
    public string customerId { get; set; }
    public int recordId { get; set; }
    public DateTime orderDate { get; set; }
    public int rentalPeriod { get; set; }
    public DateTime startDate { get; set; }
    public DateTime endDate { get; set; }
    public decimal total { get; set; }
    public virtual Customer Customer { get; set; }
    public virtual Cart Cart { get; set; }
    public List<OrderDetails> OrderDetails { get; set; }
 }

0 个答案:

没有答案