1

client class :

public partial class Client
        {

            public Client()
            {
                this.Produits = new List<Produit>();
            }
            public int idClient { get; set; }
            public string nom { get; set; }
            public string prenom { get; set; }
            public string Email { get; set; }
            public int Tel { get; set; }

            public virtual ICollection<Produit> Produits { get; set; }
        }

produit class:

public partial class Produit
    {


        public int ProduitID { get; set; }
        public string Type { get; set; }
        public string Description { get; set; }
        public int Prix { get; set; }


        public Nullable<int> client_id { get; set; }
        public virtual Client client { get; set; }
    }

view produit :

// POST: /Produit/Create
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ProduitID,Type,Description,Prix")] ProduitDTO produit)
        {

            if (ModelState.IsValid)
            {
                _ProduitService.Add(produit);
                return RedirectToAction("Index");
            }

            return View(produit);
        }

I want to use a html <select> to show client_id at the view !
Relation between classes : one client have many produit i want to show ProduitID,Type,Description,Prix and client_id

public class ClientDTO
    {
        [Key]
        [ScaffoldColumn(false)]
        public int idClient { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Nom")]
        public string nom { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Prénom")]
        public string prenom { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Email")]
        public string Email { get; set; }
        public int Tel { get; set; }
    }                

public class ProduitDTO
    {
        [Key]
        [ScaffoldColumn(false)]
        public int ProduitID { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Type")]
        public string Type { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Description")]
        public string Description { get; set; }

        [Required(ErrorMessage = "champ requis")]
        [MinLength(3, ErrorMessage = "valeur trop courte")]
        [DisplayName("Prix")]
        public int Prix { get; set; }


        public Nullable<int> client_id { get; set; }
        public virtual ClientDTO client { get; set; }
    }
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
RedOne
  • 115
  • 1
  • 1
  • 5

1 Answers1

1

You need to convert your ICollection<Produit> Produits to an IEnumerable<SelectListItem>. A great way to do this is by using a simple lambda:

IEnumerable<SelectListItem> dropdownItems = db.Client.Find(myClientId).Produits.Select(i => new SelectListItem{ Value = i.ProduitId.ToString(), Text = Produit.Description });

Attach it to your model somehow:

Model.DropDownItems = dropdownItems;

And then you can generate a dropdown-list in Razor using

@Html.DropDownListFor(Model.DropDownItems)

Voila.

Omar Himada
  • 2,540
  • 1
  • 14
  • 31