I have a table of Vendors (Vendor object) where one of the columns is a NULLABLE foreign key reference to my Accounts table (Account object). On my edit form I display the list of Accounts as a dropdown list. I have no problems setting a value nor updating the value to another account.
However, I have been unable to clear the value. Each time I do so, the UpdateModel() call or maybe the Save() call seems to fail silently (no model errors). I have tried stepping through the code but I have been unable to get consistent results so I cannot determine the exact issue but it all seems to point to the FK reference. I have tried explicitly setting the value to Nothing but that does not work.
This is my original code.
<AcceptVerbs(HttpVerbs.Post)> _
Public Function Edit(ByVal id As String, ByVal formValues As FormCollection) As ActionResult
Dim vendor = _repo.GetVendor(id)
Try
UpdateModel(vendor)
_repo.Save()
Return RedirectToAction("Index")
Catch
ModelState.AddRuleViolations(vendor.GetRuleViolations())
ViewData.Item("Accounts") = Models.DropDownPopulators.PopulateAccounts(_accounts.FindAllAccounts(), vendor.DefaultAccount)
Return View(vendor)
End Try
End Function
I even tried adding the following after the Update. I also tried doing the same to the form value before the Update.
If (vendor.DefaultAccount.Equals("")) Then vendor.DefaultAccount = Nothing