ASP.Net SKU
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using WebApplication22.Models; namespace WebApplication22.Controllers { public class DefaultController : ApiController { MyShopDB db = new MyShopDB(); public object GetAll() { var list = db.Goods_SPU.ToList(); var data = list.Select(p => new { Goods = p, Img = db.Image.Where(k => k.SPUID == p.Id ).FirstOrDefault().ImageUrl }); return data; } public object GetOne(int Id) { // 获取商品 Goods_SPU goods = db.Goods_SPU.Find(Id); // 获取商品的SPU属性 var goods_Attribute = (from S in db.SPUAttribute join A in db.Attribute on S.AID equals A.Id where A.AttributeType == 0 select new { Id = S.SPID, // 商品ID AId = S.AID, // 属性ID AType = A.InputType ,// 属性的输入类型 AName = A.AttributeName, //属性名 AValue = db.AttributeValue.Where(p => p.AttributeId == S.AID).ToList(),// 属性值列表 Value = S.Value // 实际值 }).ToList(); // 获取商品的SKU属性 var goods_SKU_Attribute = (from S in db.SPUAttribute join A in db.Attribute on S.AID equals A.Id where A.AttributeType == 1 select new { Id = S.SPID, // 商品ID AId = S.AID, // 属性ID AType = A.InputType,// 属性的输入类型 AName = A.AttributeName, //属性名 AValue = db.AttributeValue.Where(p => p.AttributeId == S.AID).ToList(),// 属性值列表 Value = S.Value // 实际值 }).ToList(); var imgs = db.Image.Where(p => p.SPUID == Id).ToList(); var sku = db.Goods_SKU.Where(p => p.SPUID == Id).ToList(); return new { Goods=goods, // 商品本身 Attr=goods_Attribute, // 商品属性 SKUAttr = goods_SKU_Attribute, // 商品库存属性 Imgs =imgs, // 商品图片 Sku=sku // 商品规格列表 }; } } }
后台
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
@{ ViewBag.Title = "Index"; } <h2>Index</h2> <div id="dv"> <table style="float:left"> <tr> <td> <img src="" width="80" height="120" /> </td> <tr /> <tr> <td> <span></span> <span></span> </td> </tr> </table> </div> <div style="clear:both"></div> <script> function load() { $.ajax({ url: 'http://localhost:56334/api/Default/GetAll', type: 'get', dataType: 'json', success: function (data) { $("#dv").empty(); $(data).each(function () { $("#dv").append( '<table style="float:left">'+ ' <tr>'+ ' <td>'+ ' <a href="/Default/Detail/'+this.Goods.Id+'"> <img src="'+this.Img+'" width="80" height="120" /></a>'+ ' </td></tr><tr>'+ ' <td>'+ ' <span>'+this.Goods.Name+'</span>'+ ' <span>'+this.Goods.Price+'</span>'+ ' </td>'+ ' </tr>'+ '</table>' ); }); } }); } load(); </script>
页面