0

我试图在一个模型中组合两个视图。但我不能让它太工作!

你能帮助我吗?

楷模:

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

我正在尝试在团队(脚手架)的详细信息页面中获取球员列表。

我已经尝试制作一个视图模型,它看起来像这样:

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

但我认为控制器是问题

控制器:

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

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

我希望你能帮我解决这个问题!多谢!

错误信息:

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

1 回答 1

1

这里的问题是您要么从控制器传递了错误的模型,要么您的视图期待错误类型的模型。

如果它是控制器,那么我相信您将需要更改类似于此的代码:

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

希望这会有所帮助。

于 2013-01-04T14:15:29.190 回答