views:

47

answers:

1

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
A: 

I 100% sure I am dealing with my VendorRepository

Private _repo As New Models.VendorRepository()

and

Namespace Models

  Public Class VendorRepository

  Private _db As New InvoicingDataContext()

  Public Function FindAllVendors() As List(Of Vendor)
    Return _db.Vendors.ToList()
  End Function

  Public Function GetVendor(ByVal id As String) As Vendor
    Return _db.Vendors.SingleOrDefault(Function(v) v.Id = id)
  End Function

  Public Sub Save()
    _db.SubmitChanges()
  End Sub

End Class

End Namespace
Jason