I'm learning C#
and ASP.NET MVC Framework
from 2 weeks and I would like to apply these knowledges through a small web application. I need your help/advices in order to set a new field between 2 classes.
Objective:
This application should be useful for my soccer team. I have a first CRUD which let to handle players. I can create players, edit players ...
I have a second CRUD which let to handle match details (datetime, pitch, match format ...).
I would like to be able to add available players for a specific match. That is to say, to be able to add a list of players from table Joueur
inside a field in the table Match
. Through this way, when I look a match in details, I cna see the list of players in order to create the team composition later with JavaScript if it's possible by moving object on a dynamic picture (not my issue for the moment).
My class Joueur
:
Very simple:
namespace FCSL.Models.Joueur
{
public class Joueur
{
[Key, Display(Name = "ID")]
public int JoueurID { get; set; }
[Required, Display(Name = "Nom"), StringLength(30)]
public string Lastname { get; set; }
[Required, Display(Name = "Prénom"), StringLength(30)]
public string Firstname { get; set; }
[Required]
[Display(Name = "Poste")]
public string Poste { get; set; }
public string Image { get; set; }
}
}
I have behind the CRUD generated by Visual Studio which works fine.
My class Match
:
namespace FCSL.Models.Match
{
public class Match
{
[Key]
public int EquipeID { get; set; }
[Required, Display(Name = "Date du match")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime MatchDay { get; set; }
[Required, Display(Name = "Heure du match")]
[DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:H:mm}")]
public DateTime MatchTime { get; set; }
[Required]
public string Adversaire { get; set; }
[Required, Display(Name = "Type de match")]
public string Format { get; set; }
[Required]
public string Terrain { get; set; }
[Display]
public string Tresse { get; set; }
[Required, Display(Name = "Mot-clé")]
public string Slug { get; set; }
}
}
Add a new field:
Now I would like a new field in my Match
class in order to select list of players from Joueur
class in a field named ListeJoueurs
.
Do I have to add this something like this ?
[Required, Display(Name = "Joueurs")]
public List<Joueur.Joueur> ListeJoueurs { get; set; }
Then, how I can display a multiple selected dropdown to display this field in my create.cshtml view file ?
Thank you very much by advance
public class Match { all old propertiese publis string ListeJoueurs { get; set; } }
ViewBag.lstCourse=your list;
@Html.DropDownListFOR(m=>m.ListeJoueurs ,ViewBag.lstCourse as selectectlsist),select"",new { @class = "form-control"})
You need to add two properties TeamOne
and TeamTwo
and in each team add property Players
as collection that hold the selected players for each team.
In your Class Match
:
namespace FCSL.Models.Match
{
public class Match
{
...
public Team TeamOne { get; set; }
public Team TeamTwo { get; set; }
}
}
Add a new class Team
:
namespace FCSL.Models.Match
{
public class Team
{
...
[Required, Display(Name = "Joueurs")]
public List<Joueur.Joueur> ListeJoueurs { get; set; }
}
}
You need also to create a view model instead of sending the model to the view, in your viewmodel you need also to transform your ListJoueurs
into MultiSelectList
And you pass your created viewmodel
throw a viewbag
like below:
public ActionResult Match()
{
...
var match = new MVCApp.Models.Match
{
AdversaryTeam = "Equipe de France",
Field = "Stade de France",
MatchDay = DateTime.Today,
MatchTime = DateTime.Now,
TeamOne = new Team
{
Players = new List<Player>
{
new Player { PlayerID = 1, Firstname = "Zineeddine", Lastname = "Zidane", Poste = "" },
new Player { PlayerID = 2, Firstname = "Roberto", Lastname = "Carlos", Poste = "" },
new Player { PlayerID = 3, Firstname = "Gianluca", Lastname = "Pagliuca", Poste = "" },
}
},
TeamTwo = new Team
{
Players = new List<Player>
{
new Player { PlayerID = 1, Firstname = "Zineeddine", Lastname = "Zidane", Poste = "" },
new Player { PlayerID = 2, Firstname = "Roberto", Lastname = "Carlos", Poste = "" },
new Player { PlayerID = 3, Firstname = "Gianluca", Lastname = "Pagliuca", Poste = "" },
}
}
};
var vm = new MatchPlayersVM
{
Match = match,
TeamOnePlayers = match.TeamOne.Players.Select(p => new SelectListItem { Text = p.Firstname, Value = p.PlayerID.ToString() }),
TeamTwoPlayers = match.TeamTwo.Players.Select(p => new SelectListItem { Text = p.Firstname, Value = p.PlayerID.ToString() }),
};
ViewBag.MatchVM = vm;
return View();
}
Finally in your create.chtml add your dropdown like below:
@using MVCApp.Models
@model MatchPlayersVM
@Html.DropDownListFor(m => m.TeamOneSelectedValues, ViewBag.MatchVM.TeamOnePlayers as IEnumerable<SelectListItem>, new { multiple = "multiple" })
@Html.DropDownListFor(m => m.TeamTwoSelectedValues, ViewBag.MatchVM.TeamOnePlayers as IEnumerable<SelectListItem>, new { multiple = "multiple" })
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.