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

  • Home
  • SEARCH
  • 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 3967258
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:41:55+00:00 2026-05-20T03:41:55+00:00

I have honestly looked for over an hour for a satisfactory answer to this

  • 0

I have honestly looked for over an hour for a satisfactory answer to this question. So it’s not that I haven’t found answers, just not ones I feel are completely correct. Which may just mean that I’m looking for a silver bullet that doesn’t exist.

In asp mvc with an EF (code first) model that includes a many to many relationship, how do I bind that relationship to checkboxes and then propagate those changes elegantly to my database.

If the view is only returning the values for checkboxes that were checked, then I no longer know which values were previously checked. Do I pull back the original object again and then compare the two? It feels dirty, probably because I’m so used to letting the EF magic manage the properties that have changed on an object.

I’ve already have a dirty version that I’m not happy with.

    [HttpPost]
    public ActionResult Edit(int id, FormCollection collection)
    {
        try
        {
            // TODO: Add insert logic here
            Program p = _programRep.GetByID(id);

            //manually update properties
            //change to model binding later


            EditPolicies(ref p, collection["SelectedPolicies"].ToString().Split(','));

            _programRep.Save(p);

            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

    private void EditPolicies(ref Program c, string[] selectedvals)
    {
        //I could do comparison logic here 
        int count = c.Policies.Count;
        for (int i = 0; i < count; i++)
        {
            c.Policies.Remove(c.Policies.ElementAt(0));
        }

        for (int i = 0; i < selectedvals.Count(); i++)
        {
            Policy g = _policyRep.GetByID(int.Parse(selectedvals[i]));
            c.Policies.Add(g);
        }
    }
  • 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-05-20T03:41:56+00:00Added an answer on May 20, 2026 at 3:41 am

    I’m not quite sure how you expect this to work so I don’t know what you are looking for but this is the correct way and by design.

    There are a lot of modeling cases. Wish I saved the link I found with the perfect example where the removal of an object from a collection simply means a relationship is missing and not that a row needs to be deleted.

    The only elegance I would add is something like:

     c.Policies.AddRange(_policyRep.GetByIDs(selectedvals[i]));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Objective-C code that I found somewhere. Honestly, I don't understand a
I honestly have no idea if this question has been asked before, or something
I wish I could have a more helpful title for this question, but honestly
I'm working on a security project in Javascript (something I honestly have not used),
Crappy title, yes. But I honestly have no idea what this particular line of
I would like to have MSTest (honestly, I'd take whatever at this point) run
I know this has been asked a lot of times, but I have honestly
I honestly have only started recently researching this so my knowledge is limited. I
I have honestly been working on this for about two weeks and researched as
Over the years I seen many people use the word generics, I honestly have

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.