ASP.NET Repeater问题

时间:2016-01-20 19:47:10

标签: c# asp.net xml serialization repeater

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

我有一个XML文件,我正在反序列化并使用转发器来显示该数据。我的问题是,位置和工资数据没有出现,其他所有XML元素都是。

我不知道如何访问JobLocations类并显示Salary and Location。

.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="$50000">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();

    }

}

1 个答案:

答案 0 :(得分:1)

您的位置和工资数据不会直接存储在Job中。它们存储在JobLocation的{​​{1}}属性中,这是一个集合对象。这就是为什么你不能像Job那样访问它们,因为有多个薪水/地点。

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

<%#Item.Salary%></div>

另一种方法是使用foreach循环,但最终看起来有点难看,所有<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> 标记都散布在各处。如果您没有停留在Web表单上,那么切换到MVC以便您可以使用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,并且您的JobLocation属性应重命名为Job.JobLocation,以符合正常的C#约定。这是因为该属性表示作业位置的集合,因此将名称复数化是有意义的。但是,该类表示单个实例,因此不要将名称复数化。