Reputation: 223
I am a college student and still learning. Here is my project; When a user logins in, they can add Products to a basket. They then proceed to the address/payment where they enter address and card details. What I need to make is a "View Orders" where a user can view previous made orders, what is the best recommended way of doing this? Not asking for all ways, just what you think is the most effective for displaying this information. The View Orders needs to display there order details, address, name etc and beneath it the products in the order. Please view my classes below for more information;
Order
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 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; }
}
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; }
}
UPDATE** Controller;
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);
}
ERROR FIXED
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; }
}
My View;
@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>
Error:
T_shirt_Company_v3.Models.OrderArchiveViewModel: : EntityType 'OrderArchiveViewModel' has no key defined. Define the key for this EntityType.
T_shirt_Company_v3.Models.OrderDetailArchive: : EntityType 'OrderDetailArchive' has no key defined. Define the key for this EntityType.
OrderArchiveViewModels: EntityType: EntitySet 'OrderArchiveViewModels' is based on type 'OrderArchiveViewModel' that has no keys defined.
OrderDetailArchives: EntityType: EntitySet 'OrderDetailArchives' is based on type 'OrderDetailArchive' that has no keys defined.
Upvotes: 0
Views: 137
Reputation: 1293
You should create new class/model which will has all info which you need. For example
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; }
}
It's just example how it can looks, use columns which you need. Then you have to get from database you data and put in into your object. Using this model you can get this by this code
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();
Create new view OrderArchive
and display it into table.
@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>
Now you need some javascript which will show order details when you click on button Details will display row with subtable with details.
$('.btnDetails').click(function(){
$(this).parent().parent().next().css('display', '');
});
Simple example: https://fiddle.jshell.net/0keogrx2/
Upvotes: 1
Reputation:
You can add View Orders
link in the user profile section. Simple way is to create a table
with Order Headers. If the user wants to drill down further then you can display Order Details. A suggestion would be to check out how ebay and amazon provide similar functionality.
Upvotes: 1