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.
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