Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand your question correctly, your int[] potentially contains ints associated with the ID of a Platform in your DB and you want to make sure your int[] contains at least one valid PlatformID, correct?</p> <p>Immediately you could do just a simple check prior to going into your logic:</p> <pre><code>// If there aren't any IDs in Platform that are in PlatformIDs... if (!_siteDB.Platforms.Any(p =&gt; PlatformIDs.Contains(p.PlatformID))) Return RedirectToAction("Index"); // And probably tell the user to check a box, if they did, // One of your checkboxes isn't matching up with your PlatformIDs </code></pre> <p>Ideally what you'd want to do is add the int[] to your model so you can check model validation. Since databased don't typically store int[], add it to your Game model. The EF probably put your DB Entities in your Models folder and if you look at them, you'll see they're partial classes. So add this in your Models folder:</p> <pre><code>public partial class Game { public Dictionary&lt;int, bool&gt; SupportedPlatforms { get; set; }// Edited } // Also add this which you'll see why below public partial class Platform { public static bool IsValidPlatformID(int PlatformID) { using (SiteDBEntities _siteDB = new SiteDBEntities()) return _siteDB.Platforms.Any(p =&gt; p.PlatformID.Equals(PlatformID)); } } </code></pre> <p>Then add a custom ValidationAttribute class:</p> <pre><code>public ContainsValidPlatformIDAttribute : ValidationAttribute { public ContainsValidPlatformIDAttribute() { } public override bool IsValid(object value) { Dictionary&lt;int, bool&gt; supportedPlatforms = (Dictionary&lt;int, bool&gt;)value; if (value == null) return true; foreach (int i in values) { if (supportedPlatforms.Values.Any(b =&gt; b.Equals(true)))// Edited return false; } return true; } </code></pre> <p>Now decorate your Property with it in the Game class:</p> <pre><code>[ContainsValidPlatformID(Error = "You did not select a valid Platform.")] public Dictionary&lt;int, bool&gt; SupportedPlatforms { get; set; }// Edited </code></pre> <p>(Edited)Now instead of hard coding a checkbox for each platform, add this instead:</p> <pre><code>&lt;%: Html.CheckboxFor(model =&gt; model.SupportedPlatforms[0]) %&gt; &lt;%: Html.ValidationMessageFor(model =&gt; model.SupportedPlatforms[0]) %&gt; </code></pre> <p>(Edited)Now your checkboxes are tied to the Model, you can validate the model in the controller, and you can remove the int[] argument from your Action method. This has all been coded from my head into this editor so you may need to tweak some things here and there but this is the direction you should be heading in when working with Models in Views.</p> <p>Also, check out what Scott Guthrie has written on the topic of <a href="http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx" rel="nofollow">MVC Model Validation</a> in his blog. Hopefully with my sample and Scott's blog, you'll be pointed in the right direction.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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