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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:40:40+00:00 2026-05-18T04:40:40+00:00

I’ve written about this a few times, but am still having difficulty seeing how

  • 0

I’ve written about this a few times, but am still having difficulty seeing how to work with many-to-many relationships in MVC2 with EF4, specifically when it comes to Create and Edit functionality. I think part of my problem is that I decided to create my database tables in such a way that the pivot tables aren’t visible in the model itself.

My tables, once again:

Games:
   int GameID (primary key, auto-incr)
   string GameTitle
   string ReviewTitle
   int Score
   int ReviewContentID (foreign key from Content - News, Articles, and Game reviews all have similar content requirements)
   int GenreID (foreign key from Genres)

Platforms:
   int PlatformID (primary key, auto-incr)
   string Name

GamePlatform (not visible in model):
   int GameID (foreign key from Games)
   int PlatformID (foreign key from Platforms)

When I create a new review, I really just want to add entries to the GamePlatform pivot table, as I’m just trying to link the game I reviewed to the already existing platforms. Dealing with it in an OOP level is confusing me as I keep thinking I’m adding to Platforms, when all I really want to do is link Games’ id to various Platform ids.

So, I don’t want to create a new Platform from the incoming HTTP-Post data. I just want to be able to take the form data and create a new Game, new review Content, and link the new Game to existing Platforms based on selected checkboxes.

I understand how to perform the first two tasks. It’s the linking I can’t seem to grasp.

Apologies for continuing to harp on this, but it’s really the one thing holding me back from making significant progress.

  • 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-18T04:40:41+00:00Added an answer on May 18, 2026 at 4:40 am

    Assuming you have correctly mapped this in entity framework and your Game object has a Platforms collection then assigning existing Platforms to a Game should be as simple as passing the IDs of those platforms to your Game add/edit action.

    In your form you can use a series of checkboxes with a value attribute of PlatformID and a single common name, ‘platformids’ for example. Note, the Html.CheckBox() HtmlHelper doesn’t have a parameter for ‘value’, so you’ll have to specify it by way of the htmlAttributes object. MVC’s default model binder will automatically group your collection of ‘platformid’ values in the form into a single typed IEnumerable by adding a matching parameter to the receiving action.

    Here’s some code to get you started:

    // games controller
    
    public action AddGame(Game newGame, int[] platformIds) {
        Platforms[] platforms;
        if(platFormIds != null && platformIds.Any()) {
            platforms = ObjectContext.Platforms.Where(ExpressionExtensions.BuildOrExpression<Platform, int>(p => p.PlatformID, platformIds)).ToList();
        }
    
        if(ModelState.IsValid()) {
            game.Platforms.AddRange(platforms);
    
            ObjectContext.AddToGames(game);
            ObjectContext.SaveChanges();
        }
    }
    
    // helper class 
    
     public static Expression<Func<TElement, bool>> BuildOrExpression<TElement, TValue>(Expression<Func<TElement, TValue>> valueSelector, IEnumerable<TValue> values) {
        if (valueSelector == null) throw new ArgumentNullException("valueSelector");
        if (values == null) throw new ArgumentNullException("values");
    
        ParameterExpression p = valueSelector.Parameters.Single();
    
        if (!values.Any())
            return e => false;
    
        IEnumerable<Expression> equals = values.Select(value => (Expression)Expression.Equal(valueSelector.Body, Expression.Constant(value, typeof(TValue))));
        Expression body = equals.Aggregate(Expression.Or);
    
        return Expression.Lambda<Func<TElement, bool>>(body, p);
    }
    

    Note: The BuildOrExpression() above is just a nice way to create the SQL equivalent of SELECT * FROM TABLE WHERE ID IN(1,2,3,4,5,...).

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.