I'm building a one to many 2 table relationship in which I will have a 'sessions' table that contains multiple entries of notes from a 'notes' table.

I am getting null values for my 'SessionId' when trying to use an HttpPost method to post notes to a specific session.

Model

public class Sessions { [Key] public int SessionId { get; set; } [Required] [Display(Name="Session Name: ")] public string SessionName { get; set; } } public class Notes { [Key] public int NotesId { get; set; } [Required] public virtual int SessionId { get; set; } [ForeignKey("SessionId")] public virtual Sessions Session { get; set; } public string Text { get; set; } } public class TheDbContext : DbContext { public DbSet<Sessions> Sessions { get; set; } public DbSet<Notes> Notes { get; set; } } public class HomeVM { public List<Sessions> SessionList { get; set; } public Sessions NewSession { get; set; } public Notes SessionId { get; set; } } public class NotesVM { public List<Notes> NotesList { get; set; } public Notes NewNote { get; set; } }

Create Notes View "Index"

@model NoteTakingv2.Models.NotesVM @{ ViewBag.Title = "Notes Session"; } <h2>Notes Session</h2> @using (Html.BeginForm()) { @Html.HiddenFor(m => m.NewNote.SessionId) @Html.EditorFor(m => m.NewNote.Text) <input type="submit" value="Create" class="btn btn-default" /> } <table> <tr> <th> <p>Notes</p> </th> <th></th> </tr> @foreach (var i in Model.NotesList) { <tr> <td> @i.Text </td> </tr> } </table>

NotesController

public class NotesController : Controller { TheDbContext db = new TheDbContext(); public ActionResult Index(int sessionId) { NotesVM model = new NotesVM(); model.NewNote = new Notes { SessionId = sessionId }; model.NotesList = db.Notes.Where(n => n.SessionId == sessionId).ToList(); return View(model); } [HttpPost] public ActionResult Index([Bind(Include = "NotesId,SessionId,Text")]Notes model) { if (ModelState.IsValid) { db.Notes.Add(model); db.SaveChanges(); return RedirectToAction("Index"); } return View(model); } }

I'm unsure where I'm going wrong, but it must be that I'm not properly obtaining the "SessionId" when posting. Any help would be greatly appreciated!