ASP.NET中继器问题

ASP.NET Repeater issue

我是 .NET 开发的新手,我遇到了 运行 一些数据未显示在我的转发器中的问题。

我有一个 XML 文件,我正在反序列化并使用中继器显示该数据。我的问题是 Location 和 Salary 数据没有显示,其他每个 XML 元素都显示了。

我不确定如何访问 JobLocations class 并显示工资和位置。

.xml

<?xml version="1.0" encoding="utf-8" ?>
<Jobs>   
  <Job Category="Administration" Title="Senior Human Resource Coordinator">
    <Description>
      <![CDATA[ Long description of the job goes here. ]]>
    </Description>
    <ShortDescription>
      <![CDATA[ Short description of the job goes here.  ]]>
    </ShortDescription>
    <JobLocations>
      <Location Salary="000">Toronto</Location>
      <Location Salary="£35000">London</Location>
    </JobLocations>
    <Benefits>Drug, Dental, Optical and Extended Healthcare Benefits</Benefits>
    <Jobtype>Full-Time</Jobtype>
  </Job>
</Job>

.cs

[Serializable]
public class Job
{        
    [XmlAttribute]
    public string Category { get; set; }
    [XmlAttribute]
    public string Title { get; set; }
    [XmlElement]
    public string Description { get; set; }
    [XmlElement]
    public string ShortDescription { get; set; }

    [XmlElement("JobLocations")]
    public List<JobLocations> JobLocation { get; set; }

    public string Benefits { get; set; }
    [XmlElement]
    public string Jobtype { get; set; }                


    public class JobLocations
    {
        [XmlAttribute("Salary")]
        public string Salary { get; set; }
        [XmlText]
        public string Location { get; set; }
    }

    public static List<Job> Load( string path )
    {
        return SerializerSupport.DeserializeList<Job>(System.IO.Path.Combine( path, "jobs.xml" ) );
    }

.aspx

<asp:Repeater ID="Job" runat="server" ItemType="Job" SelectMethod="JobGrid_GetData">
        <ItemTemplate>
          <div class="flex-item">
            <div>Title: <%#Item.Title%></div>
            <div>S.Desc: <%#Item.ShortDescription%></div>
            <div>Location: ??? </div>
            <div>Salary: ??? </div>
            <div>Category: <%#Item.Category%></div>
            <div>Jobtype: <%#Item.Jobtype%></div>
            <div>Benefits: <%#Item.Benefits%></div>  
            <div>Desc: <%#Item.Description%></div>
          </div>  
        </ItemTemplate>        
    </asp:Repeater>

.aspx.cs

public partial class JobCategories : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public IEnumerable<Job> JobGrid_GetData()
    {
        return DataCache.Instance.Jobs;
    }

    public IEnumerable<Job> Jobs_GetData()
    {
        return null;
    }

    public static IEnumerable<Job> JobGrid_DistinctCategories()
    {
        return DataCache.Instance.Jobs.GroupBy(p => p.Category).Select(g => g.First()).ToList();

    }

}

您的位置和工资数据未直接存储在 Job 中。它们存储在 JobJobLocation 属性 中,这是一个集合对象。这就是为什么你不能像 <%#Item.Salary%></div> 那样访问它们,因为有多个 salaries/locations.

根据您希望如何显示数据,您可能需要嵌套的转发器或其他显示薪水的控件。

<asp:Repeater ID="Job" runat="server" ItemType="Job" SelectMethod="JobGrid_GetData">
        <ItemTemplate>
          <div class="flex-item">
            <div>Title: <%#Item.Title%></div>
            <div>S.Desc: <%#Item.ShortDescription%></div>
            <div>
                <asp:Repeater runat="server" ItemType="JobLocations" DataSource="<%# Item.JobLocation %>">
                    <ItemTemplate>
                        <div>Location: <%#Item.Location%> </div>
                        <div>Salary: <%#Item.Salary%> </div>
                    </ItemTemplate>
                </asp:Repeater>
            </div>
            <div>Category: <%#Item.Category%></div>
            <div>Jobtype: <%#Item.Jobtype%></div>
            <div>Benefits: <%#Item.Benefits%></div>  
            <div>Desc: <%#Item.Description%></div>
          </div>  
        </ItemTemplate>        
    </asp:Repeater>

执行此操作的另一种方法是 foreach 循环,但最终看起来有点丑陋,所有 <% 标记散落各处。如果您不使用 Web 表单,那么切换到 MVC so you can use Razor markup 会有所改进。

foreach(var job in jobs)
{
    <div>Title: @job.Title</div>
    <div>S.Desc: @job.ShortDescription</div>
    ...etc...
    foreach(var jobLocation in job.JobLocation)
    {
        <div>Location: @jobLocation.Location</div>
        <div>Salary: @jobLocation.Salary</div>
    }
    <div>Category: @job.Category</div>
    ...etc...
}

您的 JobLocations class 应重命名为 JobLocation 并且您的 Job.JobLocation 属性 应重命名为 Job.JobLocations 以符合正常的 C# 约定.这是因为 属性 代表工作地点的集合,因此将名称复数化是有意义的。但是,class 表示单个实例,因此不要将名称复数化。