[英]ASP.NET Repeater issue
我是.NET开发的新手,我遇到了一个问题,即某些数据未显示在我的转发器中。
我有一个要反序列化的XML文件,并使用转发器显示该数据。 我的问题是,位置和工资数据没有显示,其他所有XML元素都没有显示。
我不确定如何访问JobLocations类并显示薪水和地点。
.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();
}
}
您的位置和薪水数据不会直接存储在Job
。 它们存储在Job
的JobLocation
属性中,该属性是一个集合对象。 这就是为什么您不能像<%#Item.Salary%></div>
那样访问它们,因为存在多个薪水/位置。
根据您希望如何显示数据,您可能需要具有嵌套的中继器或其他控件来显示工资。
<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以便可以使用Razor标记将是一个改进。
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...
}
与正常的C#约定保持一致,应该将JobLocations
类重命名为JobLocation
并将Job.JobLocation
属性重命名为Job.JobLocations
。 这是因为该属性表示作业位置的集合,因此将名称复数是有意义的。 但是,该类表示单个实例,因此请勿将名称复数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.