建立商品功能的模組
public class ItemView
{
public List<int> IdList { get; set; }
public List<ItemDetailView> ItemBlock { get; set; }
public ForPaging Paging { get; set; }
}
[MetadataType(typeof(ItemMetadata))]
public partial class Item
{
private class ItemMetadata
{
[DisplayName("商品編號")]
public int Id { get; set; }
[DisplayName("商品名稱")]
public string Name { get; set; }
[DisplayName("價格")]
public int Price { get; set; }
[DisplayName("圖片")]
public string Image { get; set; }
}
}
public Item GetDataById(int Id)
{
return db.Item.Find(Id);
}
public List<int> GetIdList(ForPaging Paging)
{
IQueryable<Item> Data = db.Item;
Paging.MaxPage = Convert.ToInt32(Math.Ceiling(
Convert.ToDouble(Data.Count()) / Paging.ItemNum));
Paging.SetRightPage();
return Data.OrderByDescending(p => p.Id)
.Skip((Paging.NowPage - 1) * Paging.ItemNum).Take(Paging.ItemNum)
.Select(p => p.Id).ToList();
}
public void Insert(Item newData)
{
db.Item.Add(newData);
db.SaveChanges();
}
public void Delete(int Id)
{
Item DeleteData = db.Item.Find(Id);
List<CartBuy> DeleteCartBuyList = DeleteData.CartBuy.ToList();
foreach (CartBuy Delete in DeleteCartBuyList)
{
db.CartBuy.Remove(Delete);
}
db.SaveChanges();
db.Item.Remove(DeleteData);
db.SaveChanges();
}