我的购物车类
public class Cart
{
[Key]
public int CartID { get; set; }
[Required]
public int BillID { get; set; }
[Required]
[ForeignKey("BillID")]
public virtual Bill Bill { get; set; }
public virtual ICollection<CartItems> Products {get; set;}
}
我的CartItems类
public class CartItems
{
[Key]
public int CartItemID { get; set; }
[Required]
public int ProductId { get; set; }
[ForeignKey("ProductId")]
public Product Product { get; set; }
[Required]
public int Qunatity { get; set; }
}
我填充内部转发器的查询如下所示:
List<AdminCartItems> items = (from c in context.Carts
where c.Bill.Date == currentDate
select new AdminCartItems
{
Products = c.Products,
BillNo = c.Bill.BillNumber,
Orderer = c.Bill.Name + ", " + c.Bill.Adress + ", " + c.Bill.PostalCode + " " + c.Bill.Country,
Sum = c.Products.Sum(x => x.Qunatity * x.Product.Price)
}).ToList();
CartRepeater.DataSource = items;
this.DataBind();
public class AdminCartItems
{
public ICollection<CartItems> Products {get; set;}
public int BillNo {get; set;}
public string Orderer {get; set; }
public double Sum {get; set;}
}
aspx页面如下所示:
<asp:Repeater ID="CartRepeater" runat="server">
<ItemTemplate>
<p><b>Broj računa: </b><%# Eval("BillNo")%></p>
<p><b>Naručioc: </b><%# Eval("Orderer")%></p>
<br />
<table class="table">
<tr>
<th><center><b>Slika</b></center></th>
<th><center><b>Proizvod</b></center></th>
<th><center><b>Količina</b></center></th>
<th><center><b>Cena</b></center></th>
<th><center><b>Ukupno</b></center></th>
</tr>
<asp:Repeater ID="ItemRepeater" runat="server" DataSource='<%# Eval("Products") %>' >
<ItemTemplate>
<tr>
<td>
<center><img src="/DBImages/<%# Eval("Product.Image") %>.png" class="productImage"/></center>
</td>
<td style="text-align:right">
<center><p><b>Proizvođač: </b><%# Eval("Product.Manufacturer.Name")%></p></center>
<center><p><b>Naziv: </b><%# DataBinder.Eval(Container.DataItem, "ProductID" %></p></center>
</td>
<td style="text-align:right">
<center><p><%# Eval("Qunatity")%></p></center>
</td>
<td style="text-align:right">
<center><p><%# Eval("Product.Price")%></p></center>
</td>
<td style="text-align:right">
<%--<center><p><%# Eval("Qunatity")%></p></center>--%>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<b style="float: right; font-size:larger; margin-right: 30px;">
Svega: <%# Eval("Sum")%> RSD
</b>
<br />
<hr />
</ItemTemplate>
</asp:Repeater>
我的问题是:如何在内部转发器中显示Product对象的属性。
我的产品对象如下所示:
public class Product
{
[Key]
public int ProductID { get; set; }
[Required]
public string Name { get; set; }
[Required]
public double Price { get; set; }
[Required]
public string Quantity { get; set; }
public string Description { get; set; }
[Required]
public bool OnSale { get; set; }
public double SalePrecent { get; set; }
[Required]
public int Image { get; set; }
[Required]
public int Stock { get; set; }
[Required]
public int CategoryID { get; set; }
[ForeignKey("CategoryID")]
public virtual Category Category { get; set; }
[Required]
public int ManufacturerID { get; set; }
[ForeignKey("ManufacturerID")]
public virtual Manufacturer Manufacturer { get; set; }
}
答案 0 :(得分:1)
当您声明外部转发器时,您应该处理OnItemDataBound事件,并在此事件中Databind您的内部转发器
外转发器:
<asp:Repeater runat="server" id="CartRepeater" OnItemDataBound="CartRepeater_ItemDataBound">
...
</asp:Repeater>
代码背后:
protected void CartRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
var item = (CartItems) e.Item.DataItem;
var repeater = (Repeater) e.Item.FindControl("ItemRepeater");
repeater.DataSource = item.Products;
repeater.DataBind();
}
您的Eval("Product.SomeProperty")
表达式不需要。从字符串中删除Product.
位,例如:
<%# Eval("Product.Image") %>
变为<%# Eval("Image") %>
答案 1 :(得分:0)
您将内部转发器绑定到产品:
<asp:Repeater ID="ItemRepeater" runat="server" DataSource='<%# Eval("Products") %>' >
因此内部转发器中的每个绑定DataItem都是Product。因此,而不是:
<center><p><b>Proizvođač: </b><%# Eval("Product.Manufacturer.Name")%></p></center>
您应该指定:
<center><p><b>Proizvođač: </b><%# Eval("Manufacturer.Name")%></p></center>
答案 2 :(得分:0)
正如您所见,我更新了问题,因此现在有一个AdminCartsItem类。外部转发器从那里获取数据源:
List<AdminCartItems> items = (from c in context.Carts
where c.Bill.Date == currentDate
select new AdminCartItems
{
Product = c.Products.Select(x => x.Product),
BillNo = c.Bill.BillNumber,
Orderer = c.Bill.Name + ", " + c.Bill.Adress + ", " + c.Bill.PostalCode + " " + c.Bill.Country,
Sum = c.Products.Sum(x => x.Qunatity * x.Product.Price),
Pices = (int)c.Products.Select(x => x.Qunatity).FirstOrDefault(),
Total = (c.Products.Select(x => x.Product.Price)).FirstOrDefault() * (int)c.Products.Select(x => x.Qunatity).FirstOrDefault()
}).ToList();
内部转发器ItemDataBound事件如下所示:
protected void CartRepeaterItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;
var item = (AdminCartItems)e.Item.DataItem;
var repeater = (Repeater)e.Item.FindControl("ItemRepeater");
repeater.DataSource = item.Product;
repeater.DataBind();
}
HTML现在看起来像这样:
<asp:Repeater ID="CartRepeater" runat="server" OnItemDataBound="CartRepeaterItemDataBound">
<ItemTemplate>
<p><b>Broj računa: </b><%# Eval("BillNo")%></p>
<p><b>Naručioc: </b><%# Eval("Orderer")%></p>
<br />
<table class="table">
<tr>
<th><center><b>Slika</b></center></th>
<th><center><b>Proizvod</b></center></th>
<th><center><b>Pakovenje</b></center></th>
<th><center><b>Cena</b></center></th>
<th><center><b>Količina</b></center></th>
<th><center><b>Ukupno</b></center></th>
</tr>
<asp:Repeater ID="ItemRepeater" runat="server" >
<ItemTemplate>
<tr>
<td>
<center><img src="/DBImages/<%# Eval("Image") %>.png" class="productImage"/></center>
</td>
<td style="text-align:right">
<center><p><b>Proizvođač: </b><%# Eval("Manufacturer.Name")%></p></center>
<center><p><b>Naziv: </b><%# Eval("Name")%></p></center>
</td>
<td style="text-align:right">
<center><p><%# Eval("Quantity")%></p></center>
</td>
<td style="text-align:right">
<center><p><%# Eval("Price")%></p></center>
</td>
<td style="text-align:right">
<center><p><%# DataBinder.Eval(Container.Parent.Parent, "DataItem.Pices")%> </p></center>
</td>
<td style="text-align:right">
<center><p><%# DataBinder.Eval(Container.Parent.Parent, "DataItem.Total")%> </p></center>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
<b style="float: right; font-size:larger; margin-right: 30px;">
Svega: <%# Eval("Sum")%> RSD
</b>
<br />
<hr />
<hr />
</ItemTemplate>
</asp:Repeater>
希望这会有所帮助。