<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 和_toUpdateArt.Supplier = Nothing 是去掉可选关系。
但它只在我调试它时有效,有时在命令的几个循环之后,在实际删除关系并将其存储在数据库中之前。
当“函数”上没有断点时,关系永远不会为空。
有没有人对此有解释以及如何解决这个问题?