There are a number of approaches that you can take. I'm going to show the POCO route, since that's how I approach MVC.
First I would make sure that the dataOffender class has a 1:M link to the charges object:
public class dataOffender
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Display(AutoGenerateField = false)]
public int Id {get; set; }
public virtual ICollection<charge> Charges { get; set; }
</charge>
I would make sure that the foreign key relationship is defined in the charges class:
public class charge
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity), Display(AutoGenerateField = false)]
public int Id { get; set; }
[Display(AutoGenerateField = false), ForeignKey("dataOffender")]
public int OffenderId { get; set; }
public virtual dataOffender Offender { get; set; }
public charge() {}
public charge(int offenderId)
{
OffenderId = offenderId;
}
}
This will establish your relationship and allow all that tasty referential integrity. Now on your controller you'll want to add an action that will allow you to create charges.
OffenderController.cs
[HttpPost]
public ActionResult Create(dataOffender offender)
{
if (ModelState.IsValid)
{
db.dataOffenders.Add(offender);
db.SaveChanges();
TempData["offenderId"] = offender.Id;
return RedirectToAction("CreateCharge","Charge");
}
return View(offender);
}
ChargeController.cs
public ActionResult CreateCharge()
{
return View((TempData["offenderId"] != null) ? new charge(TempData["offenderId"]) : new charge());
}
[HttpPost]
public ActionResult CreateCharge(charge new_charge)
{
if(ModelState.IsValid)
{
db.charges.Add(new_charge);
db.SaveChages();
return RedirectToAction("Index");
}
}
I just threw this together without testing, so I apologize for any errors.