I use ASP.NET MVC 5 and Entity Framework 6. I have a page that allows the user to enter process information. One aspect of this information is the selection of the startup process from the drop-down list. This class looks something like this:
**
public class SupportProcess { [Key] public int ProcessId { get; set; } [DisplayName("Starting process?")] public virtual SupportProcess StartProcess { get; set; } public string Name { get; set; } [DisplayName("When is this run?")] public virtual ProcessSchedule ProcessSchedule { get; set; } [DisplayName("")] public string Description { get; set; } [DisplayName("Expected Result")] public string ExpectedResult { get; set; } }
**
I use a view model that has properties for SupportProcess, the selected startup process, and a list of processes to populate the drop-down list.
public class SupportProcessViewModel { public SupportProcess SupportProcess { get; set; } public int SelectedStartProcess { get; set; } public List<SupportProcess> Processes { get; set; } public SupportProcessViewModel() { this.SupportProcess = new SupportProcess(); } }
My Edit post action looks like this:
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(SupportProcessViewModel vm) { if (ModelState.IsValid) { if (vm.SelectedStartProcess > 0) { vm.SupportProcess.StartProcess = db.SupportProcesses.Find(vm.SelectedStartProcess); } db.Entry(vm.SupportProcess).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(vm); }
The problem is that although vm.SelectedStartProcess is not null and has the correct value, it is never stored in the database. The database shows this field as StartProcess_ProcessId . It should also be noted that a process can have 0 or 1 initial processes.
I am wondering if the fact that EF made this property in the database table a foreign key, which essentially points to the same table, is somehow causing a problem. I have all the ears for suggestions.

I will also add that the Create Post action works as expected. This should be due to passing EF that the StartProcess property also needs to be updated on the object. This is an assumption though ...
[HttpPost] [ValidateAntiForgeryToken] public ActionResult Create( SupportProcessViewModel vm) { if (ModelState.IsValid) { if(vm.SelectedStartProcess > 0) { vm.SupportProcess.StartProcess = db.SupportProcesses.Find(vm.SelectedStartProcess); } db.SupportProcesses.Add(vm.SupportProcess); db.SaveChanges(); return RedirectToAction("Index"); } return View(vm); }
c # asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-6
jason
source share