<HttpPost()>
Function Edit(<Bind(Prefix:="Article")> ByVal Article As FormCollection, Optional ByVal DepartementID As Integer = 0, Optional ByVal LeverancierID As Integer = 0) As ActionResult ', ByVal ReferenceSupplierID As Integer
' Dim Art As Article = ArticleService.GetArticleById(Article.ArticleID)
Dim _toUpdateArt As Article = ArticleService.GetArticleById(Article(0))
UpdateModel(_toUpdateArt, Article)
' TryUpdateModel(Art, Article)
If LeverancierID > 0 Then
_toUpdateArt.Supplier = LeverancierService.GetLeverancierById(LeverancierID)
Else
_toUpdateArt.Supplier = Nothing 'HERE
ModelState.Remove("LeverancierID")
End If
If DepartementID > 0 Then
_toUpdateArt.Departement = DepartmentService.GetDepartmentByID(DepartementID)
Else
_toUpdateArt.Departement = Nothing 'HERE
ModelState.Remove("DepartmentID")
End If
If ModelState.IsValid Then
_toUpdateArt.EditedOn = Now
ArticleService.SaveArticle()
Return RedirectToAction("Index")
Else
ViewBag.Index = ""
ViewBag.Create = ""
' ViewBag.Edit = ""
'ViewBag.StandardValueEnabled
ViewBag.Delete = ""
ViewBag.Editable = ""
' ViewBag.LocalSearch = ""
ViewBag.Departments = DepartmentService.GetDepartments
' ViewBag.StandardValueEnabled = ""
ViewBag.MediaShow = ""
ViewData("Media") = MediaService.GetMedia.Where(Function(el) el.Reference = Domain.Common.ReferenceTo.Article And el.ReferenceID.Equals(_toUpdateArt.ArticleID)).ToList
Dim avw As New ViewModels.ArticleViewModel
With avw
.Article = _toUpdateArt
.Leveranciers = LeverancierService.GetLeveranciers.ToList.Select(Function(dl) New SelectListItem With { _
.Text = dl.RoepNaam, _
.Value = dl.LeverancierID, _
.Selected = LeverancierID = dl.LeverancierID}).ToList
.Departements = DepartmentService.GetDepartments.Select(Function(dl) New SelectListItem With { _
.Text = dl.Code, _
.Value = dl.DepartmentID, _
.Selected = DepartementID = dl.DepartmentID}).ToList
End With
Return View(avw)
End If
End Function
_toUpdateArt.Departement = Nothing and _toUpdateArt.Supplier =没有什么可以删除可选关系。
但它只在我调试它时有效,有时在命令的几个循环之后,在实际删除关系之前它存储在数据库中。
当“功能”上没有断点时,关系将永远不会被取消。
有没有人对此有解释以及如何解决这个问题?
答案 0 :(得分:2)
如果Department已经为Nothing(null),则很可能将其设置为Nothing将无效。我怀疑你看到它在调试器中工作,因为调试器访问属性并导致它延迟加载。在此之后,该属性不再是Nothing,因此将其设置为Nothing被检测为删除关系。
有几种方法可以解决这个问题: