简体   繁体   English

创建视图 - 使用产品订购

[英]Creating a view - Order with products

Im not sure how I would do this, I have an Orders class that holds users order details, address etc, and an OrderDetails class that holds the products id's and a Product class that holds all of the product info, cost, colour etc, how would I make a view to hold all of this in one view. 我不知道如何做到这一点,我有一个Orders类,它包含用户订单详细信息,地址等,还有一个OrderDetails类,它包含产品ID和一个Product类,包含所有产品信息,成本,颜色等,如何我会在一个视图中查看所有这些内容吗? Like Order 1 would have OrderDetails 1 and this would hold the products in this order. 与订单1一样,OrderDetails 1将按此顺序保留产品。

For instance I want to display the Order Details, eg address, then beneath it the products in this order. 例如,我想显示订单明细,例如地址,然后按照此顺序显示产品。 How would I go about this? 我该怎么做? Also it needs to be related to the logged in user 它还需要与登录用户相关

Here are my classes 这是我的课程

Order 订购

[Bind(Exclude = "OrderId")]
public partial class Order
{
    [ScaffoldColumn(false)]
    public int OrderId { get; set; }

    [ScaffoldColumn(false)]
    public System.DateTime OrderDate { get; set; }

    [ScaffoldColumn(false)]
    [Remote("CheckUserName", "Account")]
    public string Username { get; set; }

    [Required]
    [StringLength(16, ErrorMessage = "Your name is too long")]
    [Display(Name = "First Name")]
    public string FirstName { get; set; }

    [Required(ErrorMessage = "Your last name is required.")]
    [StringLength(16, ErrorMessage = "Last name is too long.")]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }

    [Required(ErrorMessage = "Address is required.")]
    public string Address { get; set; }

    [Required(ErrorMessage = "City is required.")]
    public string City { get; set; }

    [Required(ErrorMessage = "Postcode is required.")]
    [Display(Name = "Post Code")]
    public string PostalCode { get; set; }

    [Required(ErrorMessage = "Country is required.")]
    public string Country { get; set; }

    [Required(ErrorMessage = "Phone number is required.")]
    public string Phone { get; set; }

    [RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}", ErrorMessage = "Email doesn't look like a valid email address.")]
    public string Email { get; set; }

    [System.ComponentModel.DataAnnotations.Compare("Email")]
    [Display(Name = "Confirm your email address")]
    public string EmailConfirm { get; set; }


    [ScaffoldColumn(false)]
    public string PaymentTransactionId { get; set; }


    [ScaffoldColumn(false)]
    public bool HasBeenShipped { get; set; }

    [ScaffoldColumn(false)]
    [ReadOnly(true)]
    public decimal Total { get; set; }

    public CardDetails cardDetails { get; set; }
    //public List<CardDetails> cardDetails { get; set; }
    public List<OrderDetail> OrderDetails { get; set; }

Order Details class 订单明细类

public class OrderDetail
{
    public int OrderDetailId { get; set; }
    public int OrderId { get; set; }
    public int ProductId { get; set; }
    public int Quantity { get; set; }
    public decimal UnitPrice { get; set; }
    public virtual Product Product { get; set; }
    public virtual Order Order { get; set; }
}

The Product Class 产品类别

public class Product
{
    [Key]
    public virtual int ProductId { get; set; }
    public virtual int CategoryId { get; set; }
    public virtual string Title { get; set; }
    public virtual string Description { get; set; }
    public virtual string Colour { get; set; }
    public virtual string ProductImg { get; set; }
    public virtual decimal Price { get; set; }
    public virtual int Quantity { get; set; }

    public Category Category { get; set; }
}

So the order class would be related to the user, this would show the address etc, then i want the products related to the order beneath it 所以订单类将与用户相关,这将显示地址等,然后我想要与其下的订单相关的产品

ViewModel 视图模型

//From ORDER Table
public int OrderId { get; set; }
public System.DateTime OrderDate { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
public string Email { get; set; }
public decimal Total { get; set; }


//from Products
public List<Product> Products { get; set; }

Use a ViewModel - a class that mediates between the view and the model. 使用ViewModel - 一个介于视图和模型之间的类。

Its very common to need to have view specific data generated outside of the model - combo box items etc, and for this case you should use a View Model. 它非常常见,需要在模型外部生成视图特定数据 - 组合框项等,对于这种情况,您应该使用视图模型。 This data is semantically misplaced on the model itself, therefore you use an intermediate object to serve to the view and map this to and from your model classes - you can use a mapper such as AutoMapper to make this process a breeze. 这些数据在模型本身上在语义上是错位的,因此您使用中间对象来提供视图并将其映射到模型类和从模型类映射 - 您可以使用AutoMapper等映射器使此过程变得轻而易举。

See MVVM: 见MVVM:

https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93viewmodel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM