简体   繁体   中英

Html.RenderAction from another controller with passed parameter

I'm having 2 models: Player and PlayerAchievement

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

The Player has the Details view which show the detail information of player. I want to render ShowPlayerAchievement view in there. However, the ShowPlayerAchievement Action is in PlayerAchievement Controller

public ActionResult ShowAchievementsList(String playerID)
{
   return View(_repository.GetPlayerAchievementsByPlayerID(playerID));
}

As you see above, the method requires the playerID of the player, so I tried this code of @Html.RenderAction in Details.cshtml of Player to see if it works, but it didn't.

@Html.RenderAction("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id })

So to summarize, how can I call the ShowAchievementList action from PlayerAchievement controller with playerID parameter from the Player ?

UPDATE this code doesn't show error @Html.Action("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id });

But I got an NullRefferenceException in constructor of PlayerAchievementRepository class.

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

    }

in player_management.xml

<player_achievement>
        <number>3</number>
        <playerId>01</playerId>
        <achievementName>European Cup</achievementName>
</player_achievement>

Try one of these two syntaxes:

 @{Html.RenderAction("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id });}

or

 @Html.Action("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id });

Try the following

@{
@Html.RenderAction("ShowAchievementsList", "PlayerAchievement", new { playerID = Model.id })
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM