我有2个模特:玩家和玩家成就
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; }
//TODO: Remember to change variable name
[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 class PlayerAchievement
{
[Key]
public String playerID { get; set; }
[Required]
public String achievementName { get; set; }
[Required]
public int number { get; set; }
}
播放器具有详细信息视图,其中显示了播放器的详细信息。我想在那里渲染 ShowPlayerAchievement 视图。但是, ShowPlayerAchievement 操作位于 PlayerAchievement 控制器
public ActionResult ShowAchievementsList(String playerID)
{
return View(_repository.GetPlayerAchievementsByPlayerID(playerID));
}
如上所述,该方法需要播放器的 playerID ,所以我在播放器的Details.cshtml中尝试了 @ Html.RenderAction 的代码,看看它是否有效,但它没有。
@Html.RenderAction("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id })
更新 此代码不显示错误 @ Html.Action(" ShowAchievementsList"," PlayerAchievement",new {playerID = Model.id});
但是我在PlayerAchievementRepository类的构造函数中得到了NullRefferenceException。
public class PlayerAchievementRepository:IPlayerAchievementRepository {
public List<PlayerAchievement> allPlayerAchievements;
private XDocument playerAchievementData;
public PlayerAchievementRepository()
{
allPlayerAchievements = new List<PlayerAchievement>();
playerAchievementData = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/player_management.xml"));
//Error show here
var playerAchievements = from playerAchievement in playerAchievementData.Descendants("player_achievement")
select new PlayerAchievement((int) playerAchievement.Element("number"), playerAchievement.Element("playerID").Value,
playerAchievement.Element("achievementName").Value); allPlayerAchievements.AddRange(playerAchievements.ToList<PlayerAchievement>());
//other logic codes...
}
在player_management.xml
中<player_achievement>
<number>3</number>
<playerId>01</playerId>
<achievementName>European Cup</achievementName>
</player_achievement>
答案 0 :(得分:1)
尝试以下两种语法之一:
@{Html.RenderAction("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id });}
或
@Html.Action("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id });
答案 1 :(得分:0)
尝试以下
@{
@Html.RenderAction("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id })
}