Thắng PQ Thắng PQ - 3 months ago 7
ASP.NET (C#) Question

Attribute's value not shown in View

I'm working on ASP.NET MVC project which use XML file to be a datasource. I'm having problem in displaying attributes of virtual property.

First, I have 3 model classes

Player:

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; }
}


Achievement

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;
}
}


And Player_Achievement class:

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;
}
}


I have repositories for those 3 classes also, below is for 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));
}
}


Now. I want to see Player's achievements list. So in Index action of PlayerAchievementsController, I wrote this code:

public ActionResult Index(String id)
{
return View(_repository.GetPlayerAchievementsByPlayerID(id));
}


However, the Index view only show the number of PlayerAchievement and empty value for the image link and name. I don't know why it can get the value of 1 attribute and impossible for others.

Index view partial code:

<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>


Take a look at my project on GitHub: https://github.com/quyetnv8893/PlayerManagement

Answer

Problem sovled: I did not read the related data from PlayerAchievement, so the Achievement attribute in PlayerAchievement is currently null. To solve this, this code was updated in the Index action of 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());
}

It's the same as using dbContext.Include()

Comments