Reputation: 91
how i can multiple my tables information in one view, i want show my player club not his club_id :
Im sitting and dont understand how to do this last days, i want see result like this :
When i run this, c# throw error
I understand that error is in this row ClubName = x.BasketballClub.ClubName but i dont have any variants what i need to do ...... without clubname its works great
Upvotes: 1
Views: 175
Reputation: 726
public class PlayerIndex1Model
{
[Key]
public int Id { get; set; }
//Why this ???
public List<Player> Players { get; set; }
public string PlayerName { get; set; }
public int PlayerWeight { get; set; }
public string PlayerSurname { get; set; }
public DateTime Birthday { get; set; }
public string PlayerPosition { get; set; }
public decimal PlayerHeight { get; set; }
public string ClubName { get; set; }
[ForeignKey("BasketBallClub")]
public int BasketBallClubId { get; set; }
public virtual BasketBallClub BasketBallClub
{
get; set;
}
}
public class BasketBallClub
{
[Key]
public int Id { get; set; }
public string ClubName { get; set; }
public virtual ICollection<Player> Players { get; set; }
}
public ActionResult Index1()
{
List<Player> playerList = new List<Player>();
BasketDbContext db = new BasketDbContext();
List<Player> playerList = db.Player.ToList();
/* if not load BasketballClub try this way
var playerList = new List<Player>();
using (var db = new BasketDbContext())
{
playerList = db.Player.Include(x=>x.BasketballClub).ToList();
}
*/
PlayerIndex1Model playerVM = new PlayerIndex1Model();
List<PlayerIndex1Model> playerVMList = playerList.Select(x => new PlayerIndex1Model
{
PlayerName = x.PlayerName,
Id = x.Id,
PlayerHeight=x.PlayerHeight,
PlayerSurname=x.PlayerSurname,
PlayerPosition=x.PlayerPosition,
Birthday=x.Birthday,
PlayerWeight=x.PlayerWeight,
BasketBallClubId = x.BasketballClubId,
ClubName = x.BasketBallClub?.ClubName ?? "" //if BasketballClub is not null returns ClubName otherwise ""
}).ToList();
return View(playerVMList);
}
Fluent Api Example. Tutorial Here
public class BasketDbContext : DbContext
{
public BasketDbContext()
: base("BasketDb")
{
}
public DbSet<Player> Player { get; set; }
public DbSet<BasketBallClub> BasketBallClub { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new PlayerMap());
modelBuilder.Configurations.Add(new BasketBallClubMap());
}
}
using System.Data.Entity.ModelConfiguration;
public class PessoaMap : EntityTypeConfiguration<Player>
{
public PlayerMap()
{
// Primary Key
HasKey(t => t.Id);
// Properties
Property(t => t.PlayerName).HasMaxLength(50); //.HasColumnName("player_name");
Property(t => t.PlayerSurname).HasMaxLength(50);
Property(t => t.PlayerWeight);
//..... configure all columns
//Relationships
HasRequired(t => t.BasketBallClub)
.WithMany(t => t.Players)
.HasForeignKey(d => d.BasketBallClubId)
.WillCascadeOnDelete(false);
}
}
using System.Data.Entity.ModelConfiguration;
public class BasketBallClubMap : EntityTypeConfiguration<BasketBallClub>
{
public BasketBallClubMap()
{
// Primary Key
HasKey(t => t.Id);
// Properties
Property(t => t.Id); //.HasColumnName("id");
Property(t => t.ClubName).HasMaxLength(50); //.HasColumnName("club_name");
}
}
// Your Model
public class PlayerIndex1Model
{
public int Id { get; set; }
public string PlayerName { get; set; }
public string PlayerSurname { get; set; }
//... omitted other columns
public int BasketBallClubId { get; set; }
public virtual BasketBallClub BasketBallClub { get; set; }
}
public class BasketBallClub
{
public int Id { get; set; }
public string ClubName { get; set; }
public virtual IColletion<Player> Players { get;set; }
}
Upvotes: 1
Reputation: 91
here is My code Index1 ActionResult
public class PlayerController : Controller
{
public ActionResult Index1()
{
BasketDbContext db = new BasketDbContext();
List<Player> playerList = db.Player.ToList();
PlayerIndex1Model playerVM = new PlayerIndex1Model();
List<PlayerIndex1Model> playerVMList = playerList.Select(x => new PlayerIndex1Model
{
PlayerName = x.PlayerName,
Id = x.Id,
PlayerHeight=x.PlayerHeight,
PlayerSurname=x.PlayerSurname,
PlayerPosition=x.PlayerPosition,
Birthday=x.Birthday,
PlayerWeight=x.PlayerWeight,
BasketBallClubId = x.BasketballClubId,
ClubName = x.BasketballClub.ClubName
}).ToList();
return View(playerVMList);
}
PlayerIndex1Model.cs
using Basket.Data.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace Basket.Web.Models
{
public class PlayerIndex1Model
{
public int Id { get; set; }
public List<Player> Players { get; set; }
public string PlayerName { get; set; }
public int PlayerWeight { get; set; }
public string PlayerSurname { get; set; }
public DateTime Birthday { get; set; }
public string PlayerPosition { get; set; }
public decimal PlayerHeight { get; set; }
public string ClubName { get; set; }
//fk
[ForeignKey("BasketBallClub")]
public int BasketBallClubId { get; set; }
public virtual BasketBallClub BasketBallClub
{
get; set;
}
}
}
And cshtml
@model IEnumerable<Basket.Web.Models.PlayerIndex1Model>
@{
ViewBag.Title = "Index1";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index1</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.PlayerName)
</th>
<th>
@Html.DisplayNameFor(model => model.PlayerWeight)
</th>
<th>
@Html.DisplayNameFor(model => model.PlayerSurname)
</th>
<th>
@Html.DisplayNameFor(model => model.Birthday)
</th>
<th>
@Html.DisplayNameFor(model => model.PlayerPosition)
</th>
<th>
@Html.DisplayNameFor(model => model.PlayerHeight)
</th>
<th>
@Html.DisplayNameFor(model => model.ClubName)
</th>
<th>
@Html.DisplayNameFor(model => model.BasketBallClubId)
</th>
<th></th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.PlayerName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlayerWeight)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlayerSurname)
</td>
<td>
@Html.DisplayFor(modelItem => item.Birthday)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlayerPosition)
</td>
<td>
@Html.DisplayFor(modelItem => item.PlayerHeight)
</td>
<td>
@Html.DisplayFor(modelItem => item.ClubName)
</td>
<td>
@Html.DisplayFor(modelItem => item.BasketBallClubId)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
</tr>
}
</table>
Upvotes: 0
Reputation: 193
Set BasketBallClubId As ForeignKey in playerIndexModel which will refer to BasketBallClub Model
[ForeignKey("BasketBallClub ")]
public int BasketBallClubId { get; set; }
public virtual BasketBallClub BasketBallClub { get; set;
Now you can Access BasketBall Please See with The names of models since you haven't included the models here , hope you get an idea how to do
Upvotes: 1