title: Asp.Net MVC3 - How create Dynamic DropDownList
body: <p>I found many articles on this but still I don´t know how exactly to do this. I am trying to create my own blog engine, I have View for create article (I am using EF and Code first) and now I must fill number of category in which article should be add but I want to change it to dropdownlist with names of categories. My model looks this:</p>
  
  <pre><code>public class Article
  {
      public int ArticleID { get; set; }
      [Required]
      public string Title { get; set; }
      [Required]
      public int CategoryID { get; set; }
      public DateTime Date { get; set; }
      [Required()]
      [DataType(DataType.MultilineText)]
      [AllowHtml]
      public string Text { get; set; }
      public virtual Category Category { get; set; }
      public IEnumerable&lt;SelectListItem&gt; Categories { get; set; }
      public virtual ICollection&lt;Comment&gt; Comments { get; set; }
  }
  public class Category
  {
      public int CategoryID { get; set; }
      [Required]
      public string Name { get; set; }
      public virtual ICollection&lt;Article&gt; Articles { get; set; }
  
  }
  </code></pre>
  
  <p>I know I must use Enum (or I think) but I am not exactly sure how. I don´t know which tutorial from that I found is best for me.</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>Thanks for your answers but I found something else. I am trying this:</p>
  
  <p>This is my model:</p>
  
  <pre><code>public class Article
  {
      [Key]
      public int ArticleID { get; set; }
  
      [Display(Name = "Title")]
      [StringLength(30, MinimumLength = 5)]
      [Required]
      public string Title { get; set; }
  
      public DateTime Date { get; set; }
  
      public int CategoryID { get; set; }
  
      [Required()]
      [DataType(DataType.MultilineText)]
      [AllowHtml]
      public string Text { get; set; }
  
     public Category Category { get; set; }
  
      public virtual ICollection&lt;Comment&gt; Comments { get; set; }
  
      public IEnumerable&lt;Category&gt; Categories { get; set; }
  }
  public class Category
  {
  [Key]
      public int CategoryId { get; set; }
      [Required]
  public string CategoryName { get; set; }
      public virtual ICollection&lt;Article&gt; Articles { get; set; }
  
  }
  </code></pre>
  
  <p>This is my controller to create article:</p>
  
  <pre><code>public ActionResult Vytvorit()
  {
      IEnumerable&lt;Category&gt; categories = GetCaregories();
      var view = View(new Article() { Categories = categories });
      view.TempData.Add("Action", "Create");
  
      return view;
  
  }
  
  private static IEnumerable&lt;Category&gt; GetCaregories()
  {
      IEnumerable&lt;Category&gt; categories;
      using (BlogDBContext context = new BlogDBContext())
      {
          categories = (from one in context.Categories
                        orderby one.CategoryName
                        select one).ToList();
      }
      return categories;
  }
  
  private Category GetCategory(int categoryID)
  {
          return db.Categories.Find(categoryID);
  }
  //
  // POST: /Clanky/Vytvorit
  
  [HttpPost]
  public ActionResult Vytvorit(Article newArticle)
  {
  
      try
      {
          if (newArticle.CategoryID &gt; 0)
          {
              newArticle.Category = GetCategory(newArticle.CategoryID);
          }
          if (TryValidateModel(newArticle))
          {
                  db.Articles.Add(newArticle);
                  db.SaveChanges();
              return RedirectToAction("Index");
          }
          else
          {
              newArticle.Categories = GetCaregories();
              var view = View(newArticle);
              view.TempData.Add("Action", "Create");
              return view;
          }
      }
      catch
      {
          return View();
  
      }
  }
  </code></pre>
  
  <p>And this is part of my view:</p>
  
  <pre><code>     @Html.DropDownListFor(model =&gt; model.CategoryID, new SelectList(Model.Categories,"CategoryID","CategoryName"))
          @Html.ValidationMessageFor(model =&gt; model.CategoryID)
  </code></pre>
  
  <p>I have problem with NullReferenceExeption but I don´t know why. Can I do it this way? It looks very easy for me.</p>