[英]asp.net GridView ImageField set DataImageUrlField result of linq join
我以這種方式構建了一個GridView:
<asp:GridView runat="server" ID="gridView" class="table table-striped table-bordered gvs" AutoGenerateColumns="false" OnRowDataBound="gridView_RowDataBound">
<Columns>
<asp:ImageField DataImageUrlField="a.ProfilePicture" HeaderText="Img"></asp:ImageField>
<asp:BoundField runat="server" HeaderText="Name" DataField="a.Name"/>
...
我以這種方式將數據加載到GridView中:
gridView.DataSource=GetData();
gridView.DataBind();
方法GetData
使用Linq返回一個List:
public static List<dynamic> GetData()
{
try
{
using (var context = new Entities())
{
var entities = (from a in context.Artist
join s in context.List_StatusType on a.Type equals s.Code
where s.Table == "Artist" && s.Field == "Type"
select new { a, Tipo = s.Value }).ToList<dynamic>();
return entities;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
當我轉到頁面時,應用程序給我一個DataBind
錯誤:
System.Web.HttpException:不可能的查找字段或屬性“ a.ProfilePicture”
相反,如果我以這種方式綁定字段,那么所有字段都將完美運行。
<asp:GridView runat="server" ID="gridView" class="table table-striped table-bordered gvs" AutoGenerateColumns="false" OnRowDataBound="gridView_RowDataBound">
<Columns>
<asp:ImageField DataImageUrlField="Immagine" HeaderText="Img"></asp:ImageField>
<asp:BoundField runat="server" HeaderText="Name" DataField="a.Name"/>
...
public static List<dynamic> GetData()
{
try
{
using (var context = new Entities())
{
var entities = (from a in context.Artist
join s in context.List_StatusType on a.Type equals s.Code
where s.Table == "Artist" && s.Field == "Type"
select new { a, Immagine=a.ProfilePicture, Tipo = s.Value }).ToList<dynamic>();
return entities;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
這是什么問題 我該如何解決?
ImageField對嵌套屬性的容忍度較低。 在第一個示例中,您要投影select new { a, Tipo = s.Value }
,這將在您的案例中使用一個名為Tipo
的屬性創建一個匿名對象(動態),但至關重要的是,另一個名為a
屬性是Artist
對象。具有自己的dynamic
。 因此,要訪問a
,您需要說a.ProfilePicture
,而對於Imagine
,在第二個示例中,這是一個頂層屬性,您可以將其投影為select new { a, Immagine=a.ProfilePicture, Tipo = s.Value }
。
您可以在頂層投影所需的所有屬性,例如:var實體=(從context中的a.Artist加入s在context.List_StatusType上的a.Type等於s.Code,其中s.Table ==“ Artist” && s。字段==“類型”選擇新的{a.ProfilePicture,a.Name,Tipo = s.Value})。ToList();
更好的方法是使用所需的屬性創建一個視圖模型,以使GridView消除歧義並返回該模型的集合而不是動態模型。
public class ArtistViewModel
{
public string ProfilePicture { get; set; }
public string Name { get; set; }
public string Tipo { get; set; }
// ... and so on
}
然后在您的GetData()方法中返回
var entities = (from a in context.Artist
join s in context.List_StatusType on a.Type equals s.Code
where s.Table == "Artist" && s.Field == "Type"
select new ArtistViewModel
{
ProfilePicture= a.ProfilePicture,
Name = a.Name,
Tipo = s.Value
}).ToList();
為什么不切換到TemplateField
。 它給您更多的控制權。 然后,您可以將完整列表綁定到GridView。
<asp:TemplateField HeaderText="Img">
<ItemTemplate>
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Artist.ProfilePicture") %>' />
</ItemTemplate>
</asp:TemplateField>
請注意,Eval顯示的是嵌套類本身的名稱空間,而不是父類中屬性的名稱。
並且由於您正在使用List
,我建議您切換到強類型的GridView。 在這里查看我的答案: 如何列出從Aspx頁面Web表單的表中檢索的數據 。 然后,您可以訪問所有屬性,就像在后面的代碼中那樣。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.