1
votes

During the next time, I could create some posts because I'm learning C# and ASP.NET MVC. I'm coming from Pythonic world, so some things are not clear for me.

I would like to generate a List of strings, then I would like to display this list in my form as a DropDownList.

This is my model:

public class Joueur
    {
        public int ID { 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, StringLength(15)]
        public string Poste { get; set; }

        public string Image { get; set; }
    }

This is my controller according to Create Method:

// GET: Joueurs/Create
public ActionResult Create()
        {
            List<Strings> posteList = new List<SelectListItem>{ "Gardien", "Défenseur", "Milieu", "Attaquant" };
            ViewBag.PosteList = posteList;
            return View();
        }

And this is my view:

<div class="col-md-10">
    @*ViewBag.PosteList is holding all the postes values*@
    @Html.DropDownListFor(model => model.Poste, ViewBag.PosteList as SelectList, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Poste, "", new { @class = "text-danger" })
</div>

But I get this issue:

@Html.DropDownListFor(model => model.Poste, ViewBag.PosteList as SelectList, new { @class = "form-control" })
There is no ViewData element of type « IEnumerable » with the key « Poste ».

How I could do that ?

With Django, it's pretty easy, in my model I create a dictionary and I pass this dict in the property, but with C# ASP.NET? I don't find a way to do that.

2
You are not sending the Joueur class as a Model to the View it would seem. Seehttps://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller for an example.VDWWD

2 Answers

0
votes

I assume your view will display a form to represent the Joueur object that you want the user to fill out, and your ViewBag.PosteList will have the values that the user can select from for the Joueur.Poste property. In order to accomplish this, you should create a new/empty Joueur object in your Create controller method and pass it to the view like so:

public ActionResult Create()
{
    var model = new Joueur();
    List<Strings> posteList = new List<SelectListItem>{ "Gardien", "Défenseur", "Milieu", "Attaquant" };
    ViewBag.PosteList = posteList;
    return View(model);
}

Then the rest of your original code should work.

0
votes

I found a solution, hopefully it's a good way:

In my model I created an Enum:

public class Joueur
    {
        public int ID { 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, StringLength(15)]
        public Position Poste { get; set; }

        public string Image { get; set; }
    }

    public enum Position
    {
        Gardien,
        Défenseur,
        Milieu,
        Attaquant
    }

And in my view I added:

<div class="form-group">
    @Html.LabelFor(model => model.Poste, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.DropDownListFor(m => m.Poste, new SelectList(Enum.GetValues(typeof(FCSL.Models.Joueur.Position))), "Sélectionner le poste", new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Poste, "", new { @class = "text-danger" })
    </div>
</div>

And I applied migration commands. It seems to work now.