and when i tryed to add partial View to first one i've got an error...
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[FootballZoneMVC4.Models.TeamsViewModel]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[FootballZoneMVC4.Models.PlayersInTeamsViewModel]'.
My question is How can i display different Models in ONE VIEW?
when i tryed to add partial View to first one i've got an error...
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[FootballZoneMVC4.Models.TeamsViewModel]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[FootballZoneMVC4.Models.PlayersInTeamsViewModel]'.
@Html.Partial("Name of the PArtial", Model.WhateverPropertyFromThe_TeamsViewModel_ThatReturns_PlayersInTeamsViewModel)
<td>@Html.Partial("_ViewPlayers", Model.PlayerNames ) </td> @* This not work.. The property PlayerNames i have from _ViewPlayers partial view, but nothing hapepnd *@
</table>
When i do that i got this error..
'System.Collections.Generic.List<FootballZoneMVC4.Models.TeamsViewModel>' does not contain a definition for 'PlayerNames' and no extension method 'PlayerNames' accepting a first argument of type 'System.Collections.Generic.List<FootballZoneMVC4.Models.TeamsViewModel>'
could be found (are you missing a using directive or an assembly reference?)
If anyone know something about that please help me!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FootballZoneMVC4.DAL;
namespace FootballZoneMVC4.Models
{
public class TeamsViewModel
{
public int ID { get; set; }
public string Name { get; set; }
public string Biography { get; set; }
public string Country { get; set; }
public string ImageUrl { get; set; }
public TeamsViewModel(int id, string name, string biography, string country,
string imageUrl)
{
this.ID = id;
this.Name = name;
this.Biography = biography;
this.Country = country;
this.ImageUrl = imageUrl;
}
}
}
And This is my controller and the way how i've show my data
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FootballZoneMVC4.Models;
using FootballZoneMVC4.DAL;
namespace FootballZoneMVC4.Controllers
{
public class TeamsController : Controller
{
public ActionResult TeamDetails(int id)
{
FootballZoneMVC4Entities db = new FootballZoneMVC4Entities();
Team team = db.Teams.SingleOrDefault(t => t.TeamID == id);
return View(team);
}
public ActionResult ViewTeams()
{
ViewBag.Player = new Player();
List<Team> list = TeamsDAL.GetAllTeams();
List<TeamsViewModel> viewModel = new List<TeamsViewModel>();
foreach (var team in list)
{
viewModel.Add(new TeamsViewModel(team.TeamID, team.Name, team.Biography,
team.Country.Name, team.ImageUrl));
}
return View(viewModel);
}
}
}
And this is my function to get the teams..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FootballZoneMVC4.DAL
{
public static class TeamsDAL
{
public static List<Team> GetAllTeams()
{
FootballZoneMVC4Entities db = new FootballZoneMVC4Entities();
return db.Teams.ToList();
}
}
}
'System.Collections.Generic.List<FootballZoneMVC4.Models.TeamsViewModel>' does not contain a definition for 'PlayerNames' and no extension method 'PlayerNames' accepting a first
argument of type
1) It's giving you this error because your TeamsViewModel does not contain "PlayerNames" property. TeamsViewModel contains "Name" instead. Change PlayerNames to Name
2) You are referencing Model.PlayerNames in your actionlink, however your Model is of type List<TeamsViewModel>. This model does not contain PlayerNames, nor can you reference a List model as a single model instance. Did you mean to put this actionlink
inside your foreach loop? Then you would change it from Model.PlayerNames to item.Name
I think there's a bit of confusion here on what a ViewModel is. Your "TeamsViewModel" is not a ViewModel for the sake of containing multiple models. It's just a simple model that you've named ViewModel.
What you need is a ViewModel (FootballViewModel) that contains your Teams model (TeamModel) and a Players model (PlayerModel). Save this ViewModel to the "ViewModels" folder (you will need to create this folder at top level in your project)
using FootballZoneMVC4.Models
public class FootballViewModel
{
public List<TeamModel> Teams { get; set; }
public List<PlayerModel> Players { get; set; }
public FootballViewModel(List<TeamModel> _teams, List<PlayerModel> _players)
{
Teams = _teams;
Players = _players;
}
}
I don't expect you to understand this because none of this exists in your project yet, but this is what you should strive for. And in your view at the top, you can use:
Zitrax
Member
1 Points
9 Posts
How to show in 1 view different Models
May 30, 2012 04:19 PM|LINK
Hello all.
My problem is how in one View to display different Models from my database..
For example i have View with this model:
@using FootballZoneMVC4.Models
@model List<TeamsViewModel>
And there is the content of my partial view:
@using FootballZoneMVC4.Models
@model List<PlayersInTeamsViewModel>
and when i tryed to add partial View to first one i've got an error...
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[FootballZoneMVC4.Models.TeamsViewModel]', but this dictionary requires a model item of type 'System.Collections.Generic.List`1[FootballZoneMVC4.Models.PlayersInTeamsViewModel]'.
My question is How can i display different Models in ONE VIEW?
Or tell me another advice to do that...
Thanks in advance!
JohnLocke
Contributor
3636 Points
780 Posts
Re: How to show in 1 view different Models
May 30, 2012 04:24 PM|LINK
You are describing a "ViewModel". It's a model that allows you to add multiple models to your view.
It is a very easy model to create. Do a bit of google searching on viewmodels
ignatandrei
All-Star
137566 Points
22125 Posts
Moderator
MVP
Re: How to show in 1 view different Models
May 30, 2012 06:59 PM|LINK
@Html.Partial("Name of the PArtial", Model.WhateverPropertyFromThe_TeamsViewModel_ThatReturns_PlayersInTeamsViewModel)
Zitrax
Member
1 Points
9 Posts
Re: How to show in 1 view different Models
Jun 01, 2012 07:14 PM|LINK
I dont understand how this work, but when i type @Html.Partial("Name of the PArtial", Model.) my autocomplete its not show me any properties.
This is my TeamsViewModel and i want in him to add more ViewModels and show them to 1 page..
@using FootballZoneMVC4.Models
@model List<TeamsViewModel>
@{
ViewBag.Title = "ViewTeams";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>ViewTeams</h2>
<table>
<tr>
<td>
Name
</td>
<td>
Bio
</td>
<td>
Image
</td>
<td>
Sponsor
</td>
<td>
Country
</td>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@item.Name </td>
<td>@item.Biography </td>
<td>@item.ImageUrl </td>
<td>@item.Country </td>
</tr>
}
<td>@Html.Partial("_ViewPlayers", Model.PlayerNames ) </td> @* This not work.. The property PlayerNames i have from _ViewPlayers partial view, but nothing hapepnd *@
</table>
When i do that i got this error..
'System.Collections.Generic.List<FootballZoneMVC4.Models.TeamsViewModel>' does not contain a definition for 'PlayerNames' and no extension method 'PlayerNames' accepting a first argument of type 'System.Collections.Generic.List<FootballZoneMVC4.Models.TeamsViewModel>' could be found (are you missing a using directive or an assembly reference?)
If anyone know something about that please help me!
Thanks again for your patience!
JohnLocke
Contributor
3636 Points
780 Posts
Re: How to show in 1 view different Models
Jun 01, 2012 07:22 PM|LINK
Post your action and the model properties for TeamsViewModel
Zitrax
Member
1 Points
9 Posts
Re: How to show in 1 view different Models
Jun 01, 2012 07:28 PM|LINK
This is my Model :
using System; using System.Collections.Generic; using System.Linq; using System.Web; using FootballZoneMVC4.DAL; namespace FootballZoneMVC4.Models { public class TeamsViewModel { public int ID { get; set; } public string Name { get; set; } public string Biography { get; set; } public string Country { get; set; } public string ImageUrl { get; set; } public TeamsViewModel(int id, string name, string biography, string country, string imageUrl) { this.ID = id; this.Name = name; this.Biography = biography; this.Country = country; this.ImageUrl = imageUrl; } } }And This is my controller and the way how i've show my data
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using FootballZoneMVC4.Models; using FootballZoneMVC4.DAL; namespace FootballZoneMVC4.Controllers { public class TeamsController : Controller { public ActionResult TeamDetails(int id) { FootballZoneMVC4Entities db = new FootballZoneMVC4Entities(); Team team = db.Teams.SingleOrDefault(t => t.TeamID == id); return View(team); } public ActionResult ViewTeams() { ViewBag.Player = new Player(); List<Team> list = TeamsDAL.GetAllTeams(); List<TeamsViewModel> viewModel = new List<TeamsViewModel>(); foreach (var team in list) { viewModel.Add(new TeamsViewModel(team.TeamID, team.Name, team.Biography, team.Country.Name, team.ImageUrl)); } return View(viewModel); } } }And this is my function to get the teams..
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace FootballZoneMVC4.DAL { public static class TeamsDAL { public static List<Team> GetAllTeams() { FootballZoneMVC4Entities db = new FootballZoneMVC4Entities(); return db.Teams.ToList(); } } }JohnLocke
Contributor
3636 Points
780 Posts
Re: How to show in 1 view different Models
Jun 01, 2012 07:56 PM|LINK
OK, I see a few things
1) It's giving you this error because your TeamsViewModel does not contain "PlayerNames" property. TeamsViewModel contains "Name" instead. Change PlayerNames to Name
2) You are referencing Model.PlayerNames in your actionlink, however your Model is of type List<TeamsViewModel>. This model does not contain PlayerNames, nor can you reference a List model as a single model instance. Did you mean to put this actionlink inside your foreach loop? Then you would change it from Model.PlayerNames to item.Name
Zitrax
Member
1 Points
9 Posts
Re: How to show in 1 view different Models
Jun 01, 2012 08:30 PM|LINK
This is not the problem i know that PlayerNames doesnt exists in TeamsViewModel, but this is my purpose.
Im trying to show this PlayerNames, that have a own viewModel and trying to show them both in one view.
If i change this to just Name that have no sence, because i cant access this PlayersInTeamsViewMOdel properties.
I want to do that...
JohnLocke
Contributor
3636 Points
780 Posts
Re: How to show in 1 view different Models
Jun 02, 2012 02:18 AM|LINK
I think there's a bit of confusion here on what a ViewModel is. Your "TeamsViewModel" is not a ViewModel for the sake of containing multiple models. It's just a simple model that you've named ViewModel.
What you need is a ViewModel (FootballViewModel) that contains your Teams model (TeamModel) and a Players model (PlayerModel). Save this ViewModel to the "ViewModels" folder (you will need to create this folder at top level in your project)
using FootballZoneMVC4.Models public class FootballViewModel { public List<TeamModel> Teams { get; set; } public List<PlayerModel> Players { get; set; } public FootballViewModel(List<TeamModel> _teams, List<PlayerModel> _players) { Teams = _teams; Players = _players; } }I don't expect you to understand this because none of this exists in your project yet, but this is what you should strive for. And in your view at the top, you can use:
from there, you can loop through your teams:
foreach (var team in Model.Teams) { <td>@team.TeamName</td> }Or loop through your players:
foreach (var player in Model.Players) { <td>@player.PlayerName</td> }Zitrax
Member
1 Points
9 Posts
Re: How to show in 1 view different Models
Jun 02, 2012 03:06 PM|LINK
Thank you!!!!!!
This solve my problem perfect. I appreciate your help.
Thanks again for your patience.