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 8057405
In Process

The Archive Base Latest Questions

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

Using MVC3.0 with the razor view engine. I have an entity titled Vendors which

  • 0

Using MVC3.0 with the razor view engine. I have an entity titled “Vendors” which contains a many to many reference to another entity called “Contacts”. All I want to do is remove all of the references from a vendor in a specific controller. Here is my code:

public ActionResult _Edit(Vendor vendor)
    {
        if (ModelState.IsValid)
        {
            //Clear contact list
            vendor.Contacts.Clear();

            db.Vendors.Attach(vendor);
            db.ObjectStateManager.ChangeObjectState(vendor, EntityState.Modified);
            db.SaveChanges();
         }
    }

For some reason this doesn’t work. and I am not sure why? Thanks

  • 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:01:14+00:00Added an answer on June 5, 2026 at 9:01 am

    You should load vendor and its contacts from database, then update its properties and SaveChanges:

    var vendor = ...retrieve vendor from db...;
    vendor.Contacts.Load();
    vendor.Contacts.Clear();
    ...update vendor properties...
    db.SaveChanges();
    

    I know it is not as efficient as attaching entity and saving state, but no one said EF is perfect.

    By the way, direct binding to Entity Framework object can be very dangerous. Malicious user can prepare POST with values, that are not present in form you provided for him and change values of field, that you didn’t want to be even modified in this action. You should create special view models.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using mvc3 razor engine from view I call function with Uri.Action which return
I have created a website using MVC3 along with the Razor view engine. What
Using MVC3, C#, and the Razor View Engine: I have a form that has
I am using mvc3 and razor is my view engine how i get date
I am using Mvc3 and my view engine is razor and also writing jquery
I am building wizard step demo application with MVC3 and using razor view engine
I am using ASP.NET MVC 3 with the Razor view engine . I have
I am having difficulty rendering a partial view using the MVC3 razor engine. When
Using MVC3 and Razor View engine, I created a VB.NET web application in VS
I am trying to build a sample for myself using MVC3, Razor view engine

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.