Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8058207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T09:16:52+00:00 2026-06-05T09:16:52+00:00

<HttpPost()> Function Edit(<Bind(Prefix:=Article)> ByVal Article As FormCollection, Optional ByVal DepartementID As Integer = 0,

  • 0
<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 = Nothing is to remove the optional relationship.

But it only works when i debug it and sometimes after several loops of the command, before the relation is actually removed and it’s stored in the database.

When there isn’t a breakpoint on the “function”, the Relationship will never be nulled.

Does anyone has an explanation for this and how to fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T09:16:53+00:00Added an answer on June 5, 2026 at 9:16 am

    If Department is already Nothing (null) then most likely setting it to Nothing will have no effect. I suspect you see it working in the debugger because the debugger accesses the property and causes it to lazy load. After this the property is no longer Nothing and so setting it to Nothing is detected as deleting the relationship.

    There are a few ways to handle this:

    • Make sure that you are mapping the FK to a property in your entity (i.e. using an FK relationship) and then set the FK to null instead of setting the navigation property to null. In this case the FK is acting as a token for the relationship such that it can be unambiguously changed without the related entity needing to be loaded.
    • Always load the related entity so that the property is never null. You could do this with Include or with lazy loading.
    • If you are using EF5 on .NET 4.5 then you could start using full change tracking proxies in which case setting the property to Nothing will be detected by the proxy even if it is already Nothing. Note however that there are complications with change tracking proxies and this only works for FK relationships when using .NET 4.5.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this function in my controller. [HttpPost] public ActionResult Edit(EmployeesViewModel viewModel) { Employee
Controller First I tried this: [HttpPost] public ActionResult Edit(JournalEntry journalentry) { if (ModelState.IsValid) {
My action is : [HttpPost] public ViewResult SearchPost(FormCollection frm) { IList <post> p =db.posts.Include(user).ToList();
I have this controller: [HttpPost] public ActionResult Create(Company company) { // try to save
I have the following DeletePOST action method:- [HttpPost] public ActionResult Delete(int id) { try
This is my upload Action : [HttpPost] public ActionResult addcar(Models.vehicule model) { ViewBag.cat =
I have an action which takes an view model object [HttpPost] public String Edit(UserGroupEditViewModel
Current Solution So I have something very similar to [HttpPost] public ActionResult Upload() {
I am working to add a edit function to my MVC 3 EF model
The controller above has a standard edit ActionResult. I simply find rows in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.