MVC create using liked table values in dropdown, entity framework
<p>I have a patient model, which has a foreign key to a gender table.  I would like to create a 'Create' view which allows user to select the value of male or female from a dropdown.</p>

<p>firstly I was not sure how to get the Gender model into the dropdown.  I have managed to get it to work by putting into the viewbag, like so</p>

<pre><code>    // GET: Patient
    public ActionResult Create()
    {
        using (var context = new WaysToWellnessDB())
        {
            // prepopulat roles for the view dropdown
            var gender = context.Genders.Select(rr =&gt; new SelectListItem { Value = rr.GenderId.ToString(), Text = rr.GenderDesc }).ToList();
            ViewBag.Gender = gender;

            return View();
        }
    }
</code></pre>

<p>In my view I have the following, which gives me the dropdown I desire.</p>

<pre><code>    &lt;div class="form-group"&gt;
        @Html.LabelFor(model =&gt; model.Gender, "Gender", htmlAttributes: new { @class = "control-label col-md-2" })
        &lt;div class="col-md-10"&gt;
            @Html.DropDownList("Gender", (IEnumerable&lt;SelectListItem&gt;)ViewBag.Gender, "Select ...")
            @Html.ValidationMessageFor(model =&gt; model.Gender.GenderId, "", new { @class = "text-danger" })
        &lt;/div&gt;
    &lt;/div&gt;
</code></pre>

<p>However when I do my post to add to the database, the gender column is null.</p>

<pre><code>    // POST: /Roles/Create
    [HttpPost]
    public ActionResult Create(Patient patient)
    {
        try
        {
            using (var context = new WaysToWellnessDB())
            {
                context.Patients.Add(patient);
                context.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        catch
        {
            return View();
        }
    }
</code></pre>

<p>Please can someone advise how I get the gender Id to be stored in the database?</p>