I'm creating a web app, and I can't seem to either create or edit entries with a foreign key. I'm creating a dropdown list to edit the records, and it's being populated correctly. But anytime I edit or create a new record with that foreign key I get "The value '2' is invalid."
My Controller
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "chimeTimeID,ScheduleID,ChimeTimeStamp")] ChimeTime chimeTime)
{
ViewData["scheduleID"] = new SelectList(db.Schedules, "scheduleID", "ScheduleName", "Select a Schedule");
ViewBag.defaultModem = new SelectList(db.Schedules, "scheduleID", "ScheduleName", "Select a Schedule");
if (ModelState.IsValid)
{
db.Entry(chimeTime).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(chimeTime);
}
My View
<div class="form-group">
@Html.LabelFor(model => model.ScheduleID.ScheduleName, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="dropdown">
@Html.DropDownListFor(model => model.ScheduleID, (SelectList)ViewBag.ScheduleName, htmlAttributes: new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ScheduleID, "", new { @class = "text-danger" })
</div>
</div>
My Model
[Table("P_ChimeTime")]
public class ChimeTime
{
[Key]
public int chimeTimeID { get; set; }
public Schedule ScheduleID { get; set; }
[DataType(DataType.Time)]
[DisplayFormat(DataFormatString = "{0:T}", ApplyFormatInEditMode = true)]
public DateTime ChimeTimeStamp { get; set; }
}
So what am I doing wrong? It appears to me that MVC isn't parsing my result from the form to an int like it should.