显示有用数据的最佳方式

时间:2016-04-13 13:04:43

标签: c# asp.net-mvc

我是一名大学生,还在学习。这是我的项目; 当用户登录时,他们可以将产品添加到购物篮中。然后他们进入地址/付款,他们输入地址和卡的详细信息。我需要做的是一个"查看订单"用户可以查看以前制作的订单,推荐的最佳方式是什么?不要求所有方式,只是你认为最有效的显示这些信息。 查看订单需要显示订单详细信息,地址,名称等,并在其下方显示订单中的产品。 请查看下面的课程以获取更多信息;

顺序

 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; }
    }

OrderDetail类

 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; }
    }

产品类

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; }
}

UPDATE ** 控制器;

public ActionResult Index()
        {
        var viewModel = (from o in new TshirtStoreDB().Orders
                    select new OrderArchiveViewModel
                    {
                        Address = o.Address,
                        City = o.City,
                        OrderDate = o.OrderDate,
                        PostalCode = o.PostalCode,
                         Details = (from d in o.OrderDetails
                                    select new OrderDetailArchive
                                    {
                                        Description = d.Product.Description,
                                        Quantity = d.Quantity,
                                        Title = d.Product.Title,
                                        UnitPrice = d.UnitPrice
                                    }).ToList()
                    }).ToList() ;

            return View(viewModel);
        }

错误修复

public class TshirtStoreDB : DbContext
{

    public TshirtStoreDB() : base("name=TshirtStoreDB")
    {
    }

    public DbSet<Product> Products { get; set; }

    public DbSet<Category> Categories { get; set; }

    public DbSet<Cart> Carts { get; set; }

    public DbSet<Order> Orders { get; set; }

    public DbSet<OrderDetail> OrderDetails { get; set; }

}

我的观点;

@model T_shirt_Company_v3.ViewModels.OrderArchiveViewModel

<table>
    <tbody>
        @foreach (var m in Model)
        {
            <tr>
                <td>@m.OrderDate</td>
                <td>@m.Address</td>
                <td>@m.City</td>
                <td>@m.PostalCode</td>
                <td><button class="btnDetails">Details</button></td>
            </tr>
            foreach (var d in m.Details)
                {
                <tr style="display: none;">
                    <td>
                        <table>
                            <tbody>
                                <tr>
                                    <td>@d.Title</td>
                                    <td>@d.Description</td>
                                    <td>@d.Quantity</td>
                                    <td>@d.UnitPrice</td>
                                </tr>
                            </tbody>
                        </table>
                    </td>
                </tr>
            }
        }
    <tbody>
</table>

错误:

  

T_shirt_Company_v3.Models.OrderArchiveViewModel :: EntityType   &#39; OrderArchiveViewModel&#39;没有定义键。为此定义密钥   的EntityType。

     

T_shirt_Company_v3.Models.OrderDetailArchive :: EntityType   &#39; OrderDetailArchive&#39;没有定义键。为此定义密钥   的EntityType。

     

OrderArchiveViewModels:EntityType:EntitySet&#39; OrderArchiveViewModels&#39;   基于类型&#39; OrderArchiveViewModel&#39;没有定义键。

     

OrderDetailArchives:EntityType:EntitySet&#39; OrderDetailArchives&#39;是   基于类型&#39; OrderDetailArchive&#39;没有定义键。

2 个答案:

答案 0 :(得分:1)

您可以在用户个人资料部分添加View Orders个链接。简单的方法是使用Order Headers创建table。如果用户想要进一步向下钻取,则可以显示“订单详细信息”。建议是查看ebay和亚马逊如何提供类似的功能。

答案 1 :(得分:1)

您应该创建新的类/模型,其中包含您需要的所有信息。例如

public class OrderArchive
{
   public System.DateTime OrderDate { get; set; }
   public string Address { get; set; }
   public string City { get; set; }
   public string PostalCode { get; set; }
   public List<OrderDetailArchive> Details { get; set; }
}

public class OrderDetailArchive
{
   public string Title { get; set; }
   public string Description { get; set; }
   public int Quantity { get; set; }
   public decimal UnitPrice { get; set; }
}

它只是示例,它可以看起来,使用您需要的列。 然后你必须从数据库获取数据并放入你的对象。使用此模型,您可以通过此代码获取此代码

var list = (from o in new dbContext().Order
           select new OrderArchive
           {
                Address = o.Address,
                City = o.City,
                OrderDate = o.OrderDate,
                PostalCode = o.PostalCode
                Details = (from d in o.OrderDetails
                          select new OrderDetailArchive
                          {
                              Description = d.Product.Description,
                              Quantity = d.Quantity,
                              Title = d.Product.Title,
                              UnitPrice = d.UnitPrice
                          }).ToList()
           }).ToList();

创建新视图OrderArchive并将其显示在表格中。

@model OrderArchive

<table>
   <tbody>
   @foreach(var m in Model)
   {
      <tr>
         <td>@m.OrderDate</td>
         <td>@m.Address</td>
         <td>@m.City</td>
         <td>@m.PostalCode</td>
         <td><button class="btnDetails">Details</button></td>
      </tr>
      foreach(var d in m.Details
      {
         <tr style="display: none;">
            <td>
               <table>
                  <tbody>    
                     <tr>
                        <td>@d.Title</td>
                        <td>@d.Description</td>
                        <td>@d.Quantity</td>
                        <td>@d.UnitPrice</td>
                     </tr>
                  </tbody>
               </table>
            </td>
         </tr>
      }
   }
   <tbody>
</table>

现在您需要一些javascript,它会在您点击按钮时显示订单详情详细信息将显示包含详细信息的子行的行。

$('.btnDetails').click(function(){
   $(this).parent().parent().next().css('display', '');
});

简单示例: known