0

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.

  • `ScheduleID` is a complex object. You cannot bind a ` –  Jan 29 '18 at 21:24
  • create a property with `int` type for the foreign key `public int ScheduleID { get; set; }` and another one for `public virtual Schedule Schedule { get; set; }` for your navigational property. Use the name of the property with `int` type for your form element name. – Shyju Jan 29 '18 at 21:25
  • Your `LabelFor()` wont work either - you do not have a form control for that property –  Jan 29 '18 at 21:34
  • My LabelFor() is working properly or at least it has all the right values in it. My drop down list also has everything that's supposed to be there. But you're saying that I can't use a select with my Schedule? Because it's too complex? How exactly is it too complex? – stained.glass Jan 29 '18 at 22:06
  • A ` –  Jan 29 '18 at 22:42

1 Answers1

0

So apparently this is how it should have been:

My Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "chimeTimeID,schedule,ChimeTimeStamp")] ChimeTime chimeTime)
{
    ViewData["schedule"] = new SelectList(db.Schedules, "scheduleID", "ScheduleName", "Select a Schedule");
    ViewBag.schedule = new SelectList(db.Schedules, "scheduleID", "ScheduleName", "Select a Schedule");

    if (ModelState.IsValid)
    {
        chimeTime.ScheduleID = db.Schedules.Find (Int32.Parse (Request ["schedule"]));
        db.ChimeTimes.Add(chimeTime);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    buildChimeJob(chimeTime);

    return View(chimeTime);
}

My View:

<div class="form-group">
    @Html.LabelFor(model => model.schedule, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="dropdown">
        @Html.DropDownListFor(model => model.schedule, (SelectList)ViewBag.schedule, htmlAttributes: new { @class = "form-control" })
        @Html.ValidationMessageFor(model => model.schedule, "", new { @class = "text-danger" })
    </div>
</div>

My Model:

[Table("P_ChimeTime")]
public class ChimeTime
{
    [Key]
    public int chimeTimeID { get; set; }
    public virtual Schedule ScheduleID { get; set; }
    [DataType(DataType.Time)]
    [DisplayFormat(DataFormatString = "{0:T}", ApplyFormatInEditMode = true)]
    public DateTime ChimeTimeStamp { get; set; }
    [ForiegnKey("ScheduleID")]
    public int schedule {get; set;}
}
  • I gave you links and hints showing how to do this correctly. If you want to write bad code, go ahead, but please do not associated me with it (now edited out) –  Jan 31 '18 at 09:17