[英]Create a model class field containing a list of objects from another table in ASP.NET MVC
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. 我将从两个星期开始学习
C#
和ASP.NET MVC Framework
,我想通过一个小型Web应用程序应用这些知识。 I need your help/advices in order to set a new field between 2 classes. 我需要您的帮助/建议,以便在2个班级之间设置一个新字段。
Objective: 目的:
This application should be useful for my soccer team. 这个应用程序对我的足球队应该是有用的。 I have a first CRUD which let to handle players.
我有第一个CRUD,可以用来处理玩家。 I can create players, edit players ...
我可以创建播放器,编辑播放器...
I have a second CRUD which let to handle match details (datetime, pitch, match format ...). 我还有第二个CRUD,可以处理比赛详细信息(日期时间,音高,比赛格式...)。
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
. 也就是说,能够在表
Match
的字段内在表Joueur
添加玩家列表。 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). 通过这种方式,当我查看详细的比赛时,我会看到玩家列表,以便以后可以使用JavaScript在动态图片上移动对象(此刻不是我的问题)以创建团队组成。
My class Joueur
: 我的课程
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. 我有Visual Studio生成的CRUD,它运行正常。
My class Match
: 我的班级
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
. 现在,我想在
Match
类中添加一个新字段,以便在名为ListeJoueurs
的字段中从Joueur
类中选择球员列表。
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 ? 然后,如何显示多个选择的下拉列表以在我的create.cshtml视图文件中显示此字段?
Thank you very much by advance 提前非常感谢你
public class Match { all old propertiese publis string ListeJoueurs { get; 公共类Match {所有旧的公共场所字符串ListeJoueurs {get; set;
组; } }
}}
ViewBag.lstCourse=your list; ViewBag.lstCourse =您的列表;
@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. 您需要添加两个属性
TeamOne
和TeamTwo
并在每个团队中添加属性Players
作为集合,其中包含每个团队的选定玩家。
In your Class Match
: 在您的课堂
Match
:
namespace FCSL.Models.Match
{
public class Match
{
...
public Team TeamOne { get; set; }
public Team TeamTwo { get; set; }
}
}
Add a new class Team
: 添加新的班级
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
您还需要创建视图模型,而不是将模型发送到视图,在您的视图模型中,还需要将
ListJoueurs
转换为MultiSelectList
And you pass your created viewmodel
throw a viewbag
like below: 然后,您通过创建的
viewmodel
抛出如下所示的viewbag
:
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: 最后在您的create.chtml中添加您的下拉列表,如下所示:
@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" })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.