title: DropDownList with foreach loop in C# MVC
body: <p><strong>ORIGINAL</strong></p>
  
  <p>I have a website where I can manage cars, brands, and car models. Right now I have controllers, models and views, the application is working, everything was auto generated by Visual Studio, and i am using entity framework (database first). </p>
  
  <p>When I try to create a car, the dropdowns with brands and Car models are not cascading like I want.</p>
  
  <p>I have a solution: add a class (or other property/attribute) to each option tag on each select (dropdown). Then, with JS, i'll do the rest. I just want to know how can I do a foreach loop to build my dropdown, even if it's not the best solution, i'm not discussing that. Remember, I need to do a foreach loop to the carmodel Model, inside the Cars View.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><strong>Car View</strong></p>
  
  <pre><code>@model MyApp.Models.car
  
  @{
      ViewBag.Title = "Create";
  }
  
  &lt;h2&gt;Create&lt;/h2&gt;
  
  @using (Html.BeginForm())
  
  {
      @Html.AntiForgeryToken()
  
      &lt;div class="form-horizontal"&gt;
          &lt;h4&gt;car&lt;/h4&gt;
          &lt;hr /&gt;
          @Html.ValidationSummary(true, "", new { @class = "text-danger" })
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.bodytypeId, "bodytypeId", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("bodytypeId", null, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.bodytypeId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.manufacturerId, "manufacturerId", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("manufacturerId", null, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.manufacturerId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.modelId, "modelId", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;                
                  @Html.DropDownList("modelId", null, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.modelId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.versionId, "versionId", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("versionId", null, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.versionId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.fuelId, "fuelId", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("fuelId", null, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.fuelId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.transmissionId, "transmissionId", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("transmissionId", null, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.transmissionId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.colorId, "colorId", htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.DropDownList("colorId", null, htmlAttributes: new { @class = "form-control" })
                  @Html.ValidationMessageFor(model =&gt; model.colorId, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.horsePower, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.horsePower, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.horsePower, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.kw, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.kw, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.kw, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.cc, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.cc, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.cc, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.Co2Emissions, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.Co2Emissions, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.Co2Emissions, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.mileage, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.mileage, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.mileage, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.year, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.year, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.year, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.doors, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.doors, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.doors, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.seats, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.seats, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.seats, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.plate, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.plate, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.plate, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.price, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.price, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.price, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.shortDescription, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.shortDescription, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.shortDescription, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.longDescription, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.longDescription, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.longDescription, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.sold, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  &lt;div class="checkbox"&gt;
                      @Html.EditorFor(model =&gt; model.sold)
                      @Html.ValidationMessageFor(model =&gt; model.sold, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.active, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  &lt;div class="checkbox"&gt;
                      @Html.EditorFor(model =&gt; model.active)
                      @Html.ValidationMessageFor(model =&gt; model.active, "", new { @class = "text-danger" })
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.dateAdded, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.dateAdded, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.dateAdded, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              @Html.LabelFor(model =&gt; model.dateSold, htmlAttributes: new { @class = "control-label col-md-2" })
              &lt;div class="col-md-10"&gt;
                  @Html.EditorFor(model =&gt; model.dateSold, new { htmlAttributes = new { @class = "form-control" } })
                  @Html.ValidationMessageFor(model =&gt; model.dateSold, "", new { @class = "text-danger" })
              &lt;/div&gt;
          &lt;/div&gt;
  
          &lt;div class="form-group"&gt;
              &lt;div class="col-md-offset-2 col-md-10"&gt;
                  &lt;input type="submit" value="Create" class="btn btn-default" /&gt;
              &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  }
  
  &lt;div&gt;
      @Html.ActionLink("Back to List", "Index")
  &lt;/div&gt;
  
  @section Scripts {
      @Scripts.Render("~/bundles/jqueryval")
  }
  </code></pre>
  
  <p><strong>Car Model</strong></p>
  
  <pre><code>//------------------------------------------------------------------------------
  // &lt;auto-generated&gt;
  //     This code was generated from a template.
  //
  //     Manual changes to this file may cause unexpected behavior in your application.
  //     Manual changes to this file will be overwritten if the code is regenerated.
  // &lt;/auto-generated&gt;
  //------------------------------------------------------------------------------
  
  namespace MyApp.Models
  {
      using System;
      using System.Collections.Generic;
      using System.ComponentModel.DataAnnotations;
      using System.Web.Mvc;
      public partial class car
      {
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
          public car()
          {
              this.carimages = new HashSet&lt;carimage&gt;();
          }
  
          public int id { get; set; }
          [Display(Name = "#")]
          public Nullable&lt;int&gt; bodytypeId { get; set; }
          [Display(Name = "Body Type")]
          public Nullable&lt;int&gt; manufacturerId { get; set; }
          [Display(Name = "Model")]
          public Nullable&lt;int&gt; modelId { get; set; }
          [Display(Name = "Version")]
          public Nullable&lt;int&gt; versionId { get; set; }
          [Display(Name = "Fuel")]
          public Nullable&lt;int&gt; fuelId { get; set; }
          [Display(Name = "Transmission")]
          public Nullable&lt;int&gt; transmissionId { get; set; }
          [Display(Name = "Color")]
          public Nullable&lt;int&gt; colorId { get; set; }
          [Display(Name = "HP")]
          public Nullable&lt;int&gt; horsePower { get; set; }
          [Display(Name = "KW")]
          public Nullable&lt;int&gt; kw { get; set; }
          [Display(Name = "CC")]
          public Nullable&lt;int&gt; cc { get; set; }
          [Display(Name = "CO2")]
          public Nullable&lt;double&gt; Co2Emissions { get; set; }
          [Display(Name = "Mileage")]
          public Nullable&lt;int&gt; mileage { get; set; }
          [Display(Name = "Year")]
          public Nullable&lt;int&gt; year { get; set; }
          [Display(Name = "Doors")]
          public Nullable&lt;int&gt; doors { get; set; }
          [Display(Name = "Seats")]
          public Nullable&lt;int&gt; seats { get; set; }
          [Display(Name = "Plate")]
          public string plate { get; set; }
          [Display(Name = "Price")]
          public Nullable&lt;int&gt; price { get; set; }
          [Display(Name = "Short Description")]
          public string shortDescription { get; set; }
          [Display(Name = "Long Description")]
          public string longDescription { get; set; }
          [Display(Name = "Sold")]
          public bool sold { get; set; }
          [Display(Name = "Active")]
          public bool active { get; set; }
          [Display(Name = "Date Added")]
          [DataType(DataType.DateTime)]
          [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy hh:mm}", ApplyFormatInEditMode = true)]
          public Nullable&lt;System.DateTime&gt; dateAdded { get; set; }
          [Display(Name = "Date Sold")]
          [DataType(DataType.DateTime)]
          [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy hh:mm}", ApplyFormatInEditMode = true)]
          public Nullable&lt;System.DateTime&gt; dateSold { get; set; }
  
          public virtual bodytype bodytype { get; set; }
          public virtual color color { get; set; }
          public virtual fuel fuel { get; set; }
          public virtual manufacturer manufacturer { get; set; }
          public virtual model model { get; set; }
          public virtual transmission transmission { get; set; }
          public virtual version version { get; set; }
          [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
          public virtual ICollection&lt;carimage&gt; carimages { get; set; }
  
      }
  }
  </code></pre>
  
  <p><strong>Car Controller</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Data;
  using System.Data.Entity;
  using System.Linq;
  using System.Net;
  using System.Web;
  using System.Web.Mvc;
  using MyApp.Models;
  
  namespace MyApp.Controllers
  {
      public class carsController : Controller
      {
          private MyAppEntities db = new MyAppEntities();
  
          // GET: cars
          public ActionResult Index(string id)
          {
              string searchString = id;
              var cars = db.cars.Include(c =&gt; c.bodytype).Include(c =&gt; c.color).Include(c =&gt; c.fuel).Include(c =&gt; c.manufacturer).Include(c =&gt; c.model).Include(c =&gt; c.transmission).Include(c =&gt; c.version);
  
              if (!String.IsNullOrEmpty(searchString))
              {
                  cars = cars.Where(s =&gt; s.bodytype.name.Contains(searchString) || 
                      s.cc.ToString().Contains(searchString) ||
                      s.Co2Emissions.ToString().Contains(searchString) ||
                      s.color.name.Contains(searchString) ||
                      s.dateAdded.Value.ToString("dd-mm-yyyy").Contains(searchString) ||
                      s.dateSold.Value.ToString("dd-mm-yyyy").Contains(searchString) ||
                      s.doors.ToString().Contains(searchString) ||
                      s.fuel.name.Contains(searchString) ||
                      s.horsePower.ToString().Contains(searchString) ||
                      s.id.ToString().Contains(searchString) ||
                      s.kw.ToString().Contains(searchString) ||
                      s.longDescription.Contains(searchString) ||
                      s.manufacturer.name.Contains(searchString) ||
                      s.mileage.ToString().Contains(searchString) ||
                      s.model.name.Contains(searchString) ||
                      s.plate.Contains(searchString) ||
                      s.price.ToString().Contains(searchString) ||
                      s.seats.ToString().Contains(searchString) ||
                      s.shortDescription.Contains(searchString) ||
                      s.transmission.name.Contains(searchString) ||
                      s.version.name.Contains(searchString) ||
                      s.year.ToString().Contains(searchString)
                  );
              }
  
              return View(cars.ToList());
          }
  
          // GET: cars/Details/5
          public ActionResult Details(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              car car = db.cars.Find(id);
              if (car == null)
              {
                  return HttpNotFound();
              }
              return View(car);
          }
  
          // GET: cars/Create
          public ActionResult Create()
          {
              ViewBag.bodytypeId = new SelectList(db.bodytypes, "id", "name");
              ViewBag.colorId = new SelectList(db.colors, "id", "name");
              ViewBag.fuelId = new SelectList(db.fuels, "id", "name");
              ViewBag.manufacturerId = new SelectList(db.manufacturers, "id", "name");
              ViewBag.modelId = new SelectList(db.models, "id", "name");
              ViewBag.transmissionId = new SelectList(db.transmissions, "id", "name");
              ViewBag.versionId = new SelectList(db.versions, "id", "name");
              return View();
          }
  
          // POST: cars/Create
          // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
          // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Create([Bind(Include = "id,bodytypeId,manufacturerId,modelId,versionId,fuelId,transmissionId,colorId,horsePower,kw,cc,Co2Emissions,mileage,year,doors,seats,plate,price,shortDescription,longDescription,sold,active,dateAdded,dateSold")] car car)
          {
              if (ModelState.IsValid)
              {
                  db.cars.Add(car);
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
  
              ViewBag.bodytypeId = new SelectList(db.bodytypes, "id", "name", car.bodytypeId);
              ViewBag.colorId = new SelectList(db.colors, "id", "name", car.colorId);
              ViewBag.fuelId = new SelectList(db.fuels, "id", "name", car.fuelId);
              ViewBag.manufacturerId = new SelectList(db.manufacturers, "id", "name", car.manufacturerId);
              ViewBag.modelId = new SelectList(db.models, "id", "name", car.modelId);
              ViewBag.transmissionId = new SelectList(db.transmissions, "id", "name", car.transmissionId);
              ViewBag.versionId = new SelectList(db.versions, "id", "name", car.versionId);
              return View(car);
          }
  
          // GET: cars/Edit/5
          public ActionResult Edit(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              car car = db.cars.Find(id);
              if (car == null)
              {
                  return HttpNotFound();
              }
              ViewBag.bodytypeId = new SelectList(db.bodytypes, "id", "name", car.bodytypeId);
              ViewBag.colorId = new SelectList(db.colors, "id", "name", car.colorId);
              ViewBag.fuelId = new SelectList(db.fuels, "id", "name", car.fuelId);
              ViewBag.manufacturerId = new SelectList(db.manufacturers, "id", "name", car.manufacturerId);
              ViewBag.modelId = new SelectList(db.models, "id", "name", car.modelId);
              ViewBag.transmissionId = new SelectList(db.transmissions, "id", "name", car.transmissionId);
              ViewBag.versionId = new SelectList(db.versions, "id", "name", car.versionId);
              return View(car);
          }
  
          // POST: cars/Edit/5
          // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
          // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
          [HttpPost]
          [ValidateAntiForgeryToken]
          public ActionResult Edit([Bind(Include = "id,bodytypeId,manufacturerId,modelId,versionId,fuelId,transmissionId,colorId,horsePower,kw,cc,Co2Emissions,mileage,year,doors,seats,plate,price,shortDescription,longDescription,sold,active,dateAdded,dateSold")] car car)
          {
              if (ModelState.IsValid)
              {
                  db.Entry(car).State = EntityState.Modified;
                  db.SaveChanges();
                  return RedirectToAction("Index");
              }
              ViewBag.bodytypeId = new SelectList(db.bodytypes, "id", "name", car.bodytypeId);
              ViewBag.colorId = new SelectList(db.colors, "id", "name", car.colorId);
              ViewBag.fuelId = new SelectList(db.fuels, "id", "name", car.fuelId);
              ViewBag.manufacturerId = new SelectList(db.manufacturers, "id", "name", car.manufacturerId);
              ViewBag.modelId = new SelectList(db.models, "id", "name", car.modelId);
              ViewBag.transmissionId = new SelectList(db.transmissions, "id", "name", car.transmissionId);
              ViewBag.versionId = new SelectList(db.versions, "id", "name", car.versionId);
              return View(car);
          }
  
          // GET: cars/Delete/5
          public ActionResult Delete(int? id)
          {
              if (id == null)
              {
                  return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
              }
              car car = db.cars.Find(id);
              if (car == null)
              {
                  return HttpNotFound();
              }
              return View(car);
          }
  
          // POST: cars/Delete/5
          [HttpPost, ActionName("Delete")]
          [ValidateAntiForgeryToken]
          public ActionResult DeleteConfirmed(int id)
          {
              car car = db.cars.Find(id);
              db.cars.Remove(car);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  
          protected override void Dispose(bool disposing)
          {
              if (disposing)
              {
                  db.Dispose();
              }
              base.Dispose(disposing);
          }
      }
  }
  </code></pre>