简体   繁体   English

创建一个模型类字段,其中包含来自ASP.NET MVC中另一个表的对象列表

[英]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. 您需要添加两个属性TeamOneTeamTwo并在每个团队中添加属性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.

相关问题 C#asp.net从另一个列表创建字符串列表,该列表返回类对象的列表 - C# asp.net create string list from a another list which returns list of class objects 使用 ASP.NET MVC 从列表创建下拉字段表单 - Create Dropdown field form from List with ASP.NET MVC ASP.NET MVC-将包含数组的FormData映射到模型类 - ASP.NET MVC - Map FormData containing array to model class 使用RadioButton将模型绑定到ASP.NET MVC中的对象列表 - Model Binding To List Of Objects In ASP.NET MVC with RadioButton 按另一个表的字段对列表项进行排序 LINQ ASP.NET MVC - Sort items of list by field of another table LINQ ASP.NET MVC 将对象从Javascript绑定到ASP.NET MVC2 Controller中的模型 - binding a list of objects from Javascript to model in ASP.NET MVC2 Controller 从ASP.NET MVC中的其他模型表访问字段 - Accessing a field from a differen't model's table in ASP.NET MVC ASP.NET MVC 如何从嵌套对象的视图模型集合中动态创建表结构 - ASP.NET MVC How to create table structure dynamically in view from viewmodel collection of nested objects 如何在ASP.NET MVC 4中显示从模型到另一个视图的列表 - How do i show a list from a model to another View in ASP.NET MVC 4 ASP.NET MVC:如何将列表(从Model中的类)传递到View中的转发器? - ASP.NET MVC: How do I pass a list (from a class in Model) to a repeater in a View?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM