[英]C# asp.net create string list from a another list which returns list of class objects
[英]Create a model class field containing a list of objects from another table in ASP.NET MVC
我将从两个星期开始学习C#
和ASP.NET MVC Framework
,我想通过一个小型Web应用程序应用这些知识。 我需要您的帮助/建议,以便在2个班级之间设置一个新字段。
目的:
这个应用程序对我的足球队应该是有用的。 我有第一个CRUD,可以用来处理玩家。 我可以创建播放器,编辑播放器...
我还有第二个CRUD,可以处理比赛详细信息(日期时间,音高,比赛格式...)。
我希望能够为特定比赛添加可用的球员。 也就是说,能够在表Match
的字段内在表Joueur
添加玩家列表。 通过这种方式,当我查看详细的比赛时,我会看到玩家列表,以便以后可以使用JavaScript在动态图片上移动对象(此刻不是我的问题)以创建团队组成。
我的课程Joueur
:
很简单:
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; }
}
}
我有Visual Studio生成的CRUD,它运行正常。
我的班级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; }
}
}
添加一个新字段:
现在,我想在Match
类中添加一个新字段,以便在名为ListeJoueurs
的字段中从Joueur
类中选择球员列表。
我需要添加这样的东西吗?
[Required, Display(Name = "Joueurs")]
public List<Joueur.Joueur> ListeJoueurs { get; set; }
然后,如何显示多个选择的下拉列表以在我的create.cshtml视图文件中显示此字段?
提前非常感谢你
公共类Match {所有旧的公共场所字符串ListeJoueurs {get; 组; }}
ViewBag.lstCourse =您的列表;
@Html.DropDownListFOR(m=>m.ListeJoueurs ,ViewBag.lstCourse as selectectlsist),select"",new { @class = "form-control"})
您需要添加两个属性TeamOne
和TeamTwo
并在每个团队中添加属性Players
作为集合,其中包含每个团队的选定玩家。
在您的课堂Match
:
namespace FCSL.Models.Match
{
public class Match
{
...
public Team TeamOne { get; set; }
public Team TeamTwo { get; set; }
}
}
添加新的班级Team
:
namespace FCSL.Models.Match
{
public class Team
{
...
[Required, Display(Name = "Joueurs")]
public List<Joueur.Joueur> ListeJoueurs { get; set; }
}
}
您还需要创建视图模型,而不是将模型发送到视图,在您的视图模型中,还需要将ListJoueurs
转换为MultiSelectList
然后,您通过创建的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();
}
最后在您的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.