Я студент колледжа и до сих пор учился. Вот мой проект; Когда пользователь входит в систему, они могут добавлять продукты в корзину. Затем они переходят к адресу/платежу, где они вводят адрес и данные карты. Что мне нужно сделать, это «View Orders», когда пользователь может просматривать предыдущие заказы, каков наилучший рекомендованный способ сделать это? Не просите о всех путях, просто то, что вы считаете наиболее эффективным для отображения этой информации. В заказе View необходимо указать данные заказа, адрес, имя и т. Д. И под ним продукты в заказе. Пожалуйста, просмотрите мои классы ниже для получения дополнительной информации;Лучший способ отображать требуемые данные
Заказать
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._%+-][email protected][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);
}
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>
Ошибка:
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.
Вы спрашиваете, как идти о сохраняющихся данных в базу данных и как получить последние заказы из базы данных? Используете ли вы сначала код структуры Entity Framework? Похоже, что вы за аннотации данных, но больше информации будет оценено. – GibralterTop