title: mvc4 dropdownList insert NULL
body: <p>I search a lot here and find something like this to make dropdown list<br>
  this is my model :</p>
  
  <pre><code>public class Profits
      {
          [Key]
          public int Id { get; set; }
          public double Value { get; set; }
          public string Description{ get; set; }
         [DataType(DataType.Date)]
          public DateTime DateInput { get; set; }
          public UserProfile User { get; set; }
          public Categories CategoryName { get; set; }//foreign key
            public   int CategoryID { get; set; }
      }
  
   public class Categories
      {
          [Key]
          public int Id { get; set; }
          [Required]
          public string  Name { get; set; }
          public UserProfile User { get; set; }
  
  
      }
  </code></pre>
  
  <p>this is my controller: 
  This pass my data for dropDownList...</p>
  
  <pre><code>public ActionResult Create()
  {
      var dba = new WHFMDBContext();
      var query = dba.Categories.Select(c =&gt; new { c.Id, c.Name });
      ViewBag.Id = new SelectList(query.AsEnumerable(), "Id", "Name", 3);
      return View();
  }
  
  [HttpPost]
          [InitializeSimpleMembership]
          public ActionResult Create(Profits profits)
          {
              var user = db.UserProfiles.FirstOrDefault(x =&gt; x.UserId == WebSecurity.CurrentUserId);
              var profit = new Profits
              {
                 Value= profits.Value,
                 Description = profits.Description,
                 DateInput =profits.DateInput,
                 CategoryName =profits.CategoryName,// ???
                  User = user,
  
              };
              db.Profits.Add(profit);
              db.SaveChanges();
              return RedirectToAction("Index");
          }
  </code></pre>
  
  <p>My View : </p>
  
  <pre><code>@model WebHFM.Models.Profits
  
  
  
  @using (Html.BeginForm()) {
      @Html.ValidationSummary(true)
  
      &lt;fieldset&gt;
          &lt;legend&gt;Profits&lt;/legend&gt;
  
  
      &lt;div class="editor-field"&gt;
       @Html.LabelFor(model =&gt; model.CategoryName)
          &lt;/div&gt;
      &lt;div class="editor-field"&gt;
           @Html.DropDownList("Id", (SelectList) ViewBag.Id, "--Select One--") 
          @Html.ValidationMessageFor(model =&gt; model.CategoryName)
      &lt;/div&gt;
  
          &lt;div class="editor-label"&gt;
              @Html.LabelFor(model =&gt; model.Value)
          &lt;/div&gt;
          &lt;div class="editor-field"&gt;
              @Html.EditorFor(model =&gt; model.Value)
              @Html.ValidationMessageFor(model =&gt; model.Value)
          &lt;/div&gt;...
  </code></pre>
  
  <p>This insert data to database but <code>CategoryName_Id</code> is <code>NULL</code> what am I missing ?
  and <code>CategoryName = profits.CategoryName</code> this is a foreign key to Categories in Profits  <code>public Categories CategoryName { get; set; }</code> </p>