I have a small model called GoalTypes which would contain stuff like "Running, Cycling, Weight.." etc
public class GoalType
{
public int Id { get; set; }
public string Type { get; set; }
}
I also have a model called Goals public class Goals { public int Id { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime StartDate { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? EndDate { get; set; }
public string Type { get; set; }
public double Level { get; set; }
} The type field would be populated by the Type field from GoalTypes. In my goals controller I have done:
public ActionResult Create()
{
ViewBag.listOfGoals = new SelectList(db.GoalTypes, "Id", "Type");
return View();
}
and in my view
<div class="form-group">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("listOfGoals", "Select a Goal")
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
</div>
this populates the dropdown but if i submit it, the type field is left blank