我正在使用ASP.NET MVC项目,该项目使用XML文件作为数据源。我在显示虚拟财产的属性方面遇到了问题。
首先,我有3个模型类
播放器:
public class Player
{
[Required]
public String clubName { get; set; }
[Required]
public String id { get; set; }
[Required]
public int number { get; set; }
[Required]
public String name { get; set; }
[Required]
public String position { get; set; }
[Required]
public DateTime dateOfBirth { get; set; }
[Required]
public string placeOfBirth { get; set; }
[Required]
public double weight { get; set; }
[Required]
public double height { get; set; }
[Required]
public String description { get; set; }
[Required]
public String imageLink { get; set; }
[Required]
public Boolean status { get; set; }
public virtual ICollection<PlayerAchievement> achievements { get; set; }
}
成就
public class Achievement
{
[Key]
public String name { get; set; }
[Required]
public String imageLink { get; set; }
public virtual ICollection<PlayerAchievement> playerAchievements { get; set; }
public Achievement()
{
this.name = null;
this.imageLink = null;
}
public Achievement(String name, String imageLink)
{
this.name = name;
this.imageLink = imageLink;
}
}
和Player_Achievement类:
public class PlayerAchievement
{
[Key]
public String playerId { get; set; }
[Required]
public String achievementName { get; set; }
[Required]
public int number { get; set; }
public virtual Player player { get; set; }
public virtual Achievement achievement { get; set; }
public PlayerAchievement()
{
this.playerId = null;
this.achievementName = null;
this.number = 0;
}
public PlayerAchievement(int number, String playerId, String achievementName)
{
this.playerId = playerId;
this.achievementName = achievementName;
this.number = number;
}
}
我还有3个类的存储库,下面是PlayerAchievement:
public class PlayerAchievementRepository : IPlayerAchievementRepository
{
public List<PlayerAchievement> allPlayerAchievements;
private XDocument playerAchievementData;
public PlayerAchievementRepository()
{
//Code get all player_achievement in xml file
}
public IEnumerable<PlayerAchievement> GetPlayerAchievementsByPlayerID(String id)
{
return allPlayerAchievements.FindAll(item => item.playerId.Equals(id));
}
}
现在。我想看看球员的成就列表。所以在PlayerAchievementsController的Index动作中,我写了这段代码:
public ActionResult Index(String id)
{
return View(_repository.GetPlayerAchievementsByPlayerID(id));
}
但是,“索引”视图仅显示图像链接和名称的PlayerAchievement数和空值。我不知道为什么它可以获得1个属性的值而对其他人来说是不可能的。
索引视图部分代码:
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.achievement.imageLink)
</th>
<th>
@Html.DisplayNameFor(model => model.number)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.achievement.imageLink)
</td>
<td>
@Html.DisplayFor(modelItem => item.number)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.playerId }) |
@Html.ActionLink("Details", "Details", new { id=item.playerId }) |
@Html.ActionLink("Delete", "Delete", new { id=item.playerId })
</td>
</tr>
在GitHub上查看我的项目:https://github.com/quyetnv8893/PlayerManagement
答案 0 :(得分:0)
我从git hub获得了您的代码副本,但稍有变化,请将AchievementRepository.cs
第39行代码上的代码更改为
achievementData.Descendants("achievements").FirstOrDefault().Add(new XElement("achievement",
new XElement("name", achievement.name), new XElement("imageLink", achievement.imageLink)));
正如您之前的代码创建了无效的xml标记层次结构。
答案 1 :(得分:0)
问题解决:我没有从PlayerAchievement 读取相关数据,因此PlayerAchievement中的Achievement属性当前为null。为解决此问题,此代码已在PlayerAchievement的索引操作中更新
public ActionResult Index(String id)
{
private IAchievementRepository _achievementRepository = new AchievementRepository();
var playerAchievements = _repository.GetPlayerAchievementsByPlayerID(id)
.Where(achievement => achievement.PlayerID.Equals(id));
foreach (var achievement in playerAchievements)
{
achievement.Achievement = _achievementRepository.GetAchievementByName(achievement.AchievementName);
}
return View(playerAchievements.ToList());
}
与使用dbContext.Include()
相同