0

I'm trying to combine two views in one model. But I cannot get it too work!

Can you help me?

Models:

 public class Player
{
    public int playerID { get; set; }

    [Required]
    [Display(Name = "Spelernaam:")]
    public String playerName { get; set; }

    [Display(Name = "Team:")]
    public int teamID { get; set; }

    public virtual Team Team { get; set; }

    [Required]
    [Display(Name = "Waarde:")]
    public int playerValue { get; set; }
}

public class Team
{
    public int teamID { get; set; }

    [Required]
    [Display(Name = "Teamnaam:")]
    public String teamName { get; set; }

    [Required]
    [Display(Name = "Teamwaarde:")]
    [Range(0, 100, ErrorMessage = "Waarde moet tussen de 0 en de 100 liggen")]
    public int teamValue { get; set; }

    [Required]
    [Display(Name = "Teambudget:")]
    [Range(0, 100000000, ErrorMessage = "Waarde moet tussen de 0 en de 100000000 liggen")]
    public int teamBudget { get; set; }

    [DefaultValue(0)]
    public int gamePoints { get; set; }

    public virtual ICollection<Player> Player { get; set; }
}

I'm trying to get a list of players in a detail page of a team (scaffold).

I have already tried to make a viewmodel and it looks like this:

public class PlayersPerTeam
{
        public Team Team { get; set; }
        public Player Player { get; set; }
}

But i think the controller is the problem

Controller:

//
    // GET: /Team/Details/5

    public ActionResult Details(int id = 0)
    {
        Team team = db.Teams.Find(id);
        if (team == null)
        {
            return HttpNotFound();
        }
        return View(team);
    }

I hope you can help me with this! thanks alot!

Error message:

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Team_B041141ED3453289BD09E3CAC14C091B831E03C09FDA76FF2D783BDDCE179358', but this dictionary requires a model item of type 'HerkansingsOpdracht.Models.PlayersPerTeam'.
tereško
  • 58,060
  • 25
  • 98
  • 150
Bvweijen
  • 73
  • 1
  • 8
  • 1
    Are you sure that you're not somehow implementing lazy loading with your db pull for `Team`? If so, you're definitely going to get `team.Player` as `null`. – Richard Neil Ilagan Jan 04 '13 at 13:37
  • `But i think the controller is the problem` - what's the problem? You haven't mentioned anything about a problem in your question. – Darin Dimitrov Jan 04 '13 at 13:43
  • This is the error: The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.Team_B041141ED3453289BD09E3CAC14C091B831E03C09FDA76FF2D783BDDCE179358', but this dictionary requires a model item of type 'HerkansingsOpdracht.Models.PlayersPerTeam'. – Bvweijen Jan 04 '13 at 14:08

1 Answers1

1

The problem here is that you are either passing the wrong model through from the controller, or your view is expecting the wrong type of model.

If its the controller then i believe you will need to change your code similar to this:

public ActionResult Details(int id = 0)
{
    PlayersPerTeam team = new PlayersPerTeam{Team = db.Teams.Find(id)};
    if (team == null)
    {
        return HttpNotFound();
    }
    return View(team);
}

Hopefully this helps.

Gaz Winter
  • 2,924
  • 2
  • 25
  • 47