【问题标题】:how to use asp.net repeater with list of dictionary?如何使用带有字典列表的 asp.net 中继器?
【发布时间】:2014-11-08 11:41:48
【问题描述】:

我有一个字典列表,我这样创建它:

List<Dictionary<String, String>> _list = new List<Dictionary<string, string>>();
            var q = from d in db.TT_DELIVERies
                    where d.DeliveryOrderNumber == donumber
                    select new { d.DeliveryId };
            if (q.Count() > 0)
            {
                var qd = from d in db.TT_DELIVERY_REQUESTs
                         where d.DeliveryOrderId == q.ToList()[0].DeliveryId
                         select new { d.ItemDescription, d.PackageDescription };

                Console.Write(qd.Count());
                if (qd.Count() > 0)
                {
                    foreach (var r in qd)
                    {
                        Dictionary<String, String> temp = new Dictionary<string, string>();
                        temp.Add("Name", r.ItemDescription);
                        temp.Add("Desc", r.PackageDescription);
                        _list.Add(temp);
                    }
                }
            }

我尝试像这样与中继器绑定:

rptAddedDocs.DataSource = _list;
                rptAddedDocs.DataBind();

这是我的中继器

<asp:Repeater ID="rptAddedDocs" runat="server">

                            <HeaderTemplate>
                                <table id="tblListAddedDocs" class="table table-condensed table-hover">
                            <thead>
                                <tr>
                                    <th>No</th>
                                    <th>Document Name</th>
                                    <th>Quantity</th>
                                    <th>UOM</th>
                                    <th>Description</th>
                                    <th></th>
                                </tr>
                            </thead>
                            </HeaderTemplate>

                            <ItemTemplate>
                                <tr>
                                    <td>1</td>
                                    <td><%# Eval("Name")%> </td>
                                    <td>1 </td>
                                    <td>Envlope </td>
                                    <td><%#Eval("Desc")%> </td>
                                    <td></td>
                                </tr>
                            </ItemTemplate>

                            <FooterTemplate>
                                </table>
                            </FooterTemplate>

                        </asp:Repeater>

但我得到这样的错误:

DataBinding: 'System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]' does not contain a property with the name 'Name'.

请帮助我如何正确使用带有字典列表的中继器?

【问题讨论】:

    标签: c# asp.net dictionary


    【解决方案1】:

    在你的 .aspx 文件中你应该这样写:

    <%#((System.Collections.Generic.Dictionary<string, string>)Container.DataItem)["Name"]%>
    

    【讨论】:

      【解决方案2】:

      您似乎混淆了Dictionary&lt;TKey, TValue&gt; 的用途。字典是一种用于存储键/值对的数据结构,其中键是唯一的。 Dictionary.Add() 方法的第一个属性是这个键,而不是第二个参数中数据的任意名称。如果您选择保留现有数据结构,则实际上不需要List&lt;Dictionary&lt;string, string&gt;&gt;,而从外观上看需要一个简单的Dictionary&lt;string, string&gt;,并且您需要将数据绑定在“Key”和“Value”上。

      看起来你实际上试图做的是创建一个不存在类型的列表,一个具有名为Namestring 属性,另一个名为@987654327 @。最简单的做法是创建您自己的类,并使用以下定义:

      public class MyItem
      {
          public string Name { get; set; }
          public string Desc { get; set; }
      }
      

      然后将_list 变量声明为List&lt;MyItem&gt; 的实例,并将foreach 循环中的人口更改为

      _list.Add(new MyItem
      {
          Name = r.ItemDescription;
          Desc = r.PackageDescription;
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-03
        • 2015-12-21
        • 2022-11-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多