title: Save newly created object ASP.NET MVC
body: <p>I'm using EF Code First and here is my class which references a Professor object.</p>
  
  <pre><code>public class Mark
      {
          public int MarkId { get; set; }
          [Required(ErrorMessage = "Please provide Grade")]
          [Display(Name = "Grade Value")]
          public int MarkValue { get; set; }
  
          [Required(ErrorMessage = "Please provide grade type")]
          [Display(Name = "Grade Type")]
          public string Type { get; set; }
  
          [Required(ErrorMessage = "Please provide Subject")]
          [Display(Name = "Subject")]
          public virtual Subject Subject { get; set; }
          public virtual User User { get; set; }
          public virtual Professor Professor { get; set; }
  
  
      }
  </code></pre>
  
  <p>In my view I have a dropdown to select a professor who put the mark.
  I implemented it like this:</p>
  
  <pre><code>//controller
   public MarksController() : this(new MarkRepository())
          {
              IProfessorRepository professorsRepo = new ProfessorRepository();
              List&lt;Professor&gt; professors = professorsRepo.All.ToList&lt;Professor&gt;();
  
              List&lt;SelectListItem&gt; items = new List&lt;SelectListItem&gt;();
              foreach (Professor p in professors)
              {
                  items.Add(new SelectListItem 
                  { 
                      Text = String.Concat(p.FirstName, " ", p.LastName), Value = p.ProfessorId.ToString() 
                  });
              }
  
              items.Add(new SelectListItem
              {
                  Text = "Please select Professor", Value = "0", Selected = true
              });
  
              ViewBag.Professors = items;
          }
  //view create or edit
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.MarkValue)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(model =&gt; model.MarkValue)
      @Html.ValidationMessageFor(model =&gt; model.MarkValue)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.Type)
  &lt;/div&gt;
  &lt;div class="editor-field"&gt;
      @Html.EditorFor(model =&gt; model.Type)
      @Html.ValidationMessageFor(model =&gt; model.Type)
  &lt;/div&gt;
  
  &lt;div class="editor-label"&gt;
      @Html.LabelFor(model =&gt; model.Professor)
  &lt;/div&gt;
  
  &lt;div class="editor-field"&gt;
      @Html.DropDownList("Professors");
  &lt;/div&gt;
  </code></pre>
  
  <p>Yes, I know that there is is a metod in Controller called Create which appeals repository method InsertOrUpdate.</p>
  
  <pre><code>[HttpPost]
          public ActionResult Create(Mark mark)
          {
              if (ModelState.IsValid) {
                  markRepository.InsertOrUpdate(mark);
                  markRepository.Save();
                  return RedirectToAction("Index");
              } else {
                  return View();
              }
          }
  
  
   public void InsertOrUpdate(Mark mark)
          {
              if (mark.MarkId == default(int)) {
                  // New entity
                  context.Marks.Add(mark);
              } else {
                  // Existing entity
                  context.Entry(mark).State = EntityState.Modified;
              }
          }
  </code></pre>
  
  <p>Everything seems logic, but how can I save a newly created Mark object with the professor selected? How does ASP.NET arranges the object which is passed to Create method on Controller? How should I match the selected professor with the mark I am creating?</p>