Note that there are some explanatory texts on larger screens.

plurals
  1. POHow does one do MVC2/EF4 EntityCollection validation w/ data annotations?
    primarykey
    data
    text
    <p>I have finally gotten over one hurdle and can now successfully create new model data. Now there's another catch - validation. Most of the validation seems easy enough as a lot of my model data are scalar values. There is a many-to-many relationship I link to, however, so I'm not sure how to go about validating that. My model is (once again):</p> <pre><code>Game (only listing the relevant columns): GameID - int (primary key, auto-incr) Platform: PlatformID - int (primary key, auto-incr) Name - string GamePlatform (not a visible entity): GameID - int (foreign key from Games) PlatformID - int (foreign key from Platforms) </code></pre> <p>And my Create method (yes, I know it's sloppy and amateurish - I <em>am</em> an amateur and trying to learn. I'll definitely add error checking to it. I'm just trying to get the big picture of the view->controller->validation->persist-in-db/show errors process down):</p> <pre><code>public ActionResult CreateReview([Bind(prefix = "GameData")]Game newGame, int[] PlatformIDs) { try { foreach(int i in PlatformIDs) { Platform plat = _siteDB.Platforms.Single(p =&gt; p.PlatformID == i); newGame.Platforms.Add(plat); } newGame.LastModified = Datetime.Now; _siteDB.Games.AddObject(newGame); _siteDB.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } } </code></pre> <p>The array of PlatformIDs are supplied by a group of checkboxes within my view. For my Game to be valid, it must be associated with at least one Platform. I'm just not sure how to validate that with data annotations, or if it's even possible to do so. Any help would be greatly appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload