0

Le titre à peu près tout dit. J'ai un objet entité EF4 complexe qui a une liste d'objets plus petits que je voudrais lier aux cases à cocher dans ma vue. Je ne peux juste pas comprendre comment satisfaire le premier argument de Html.CheckboxFor(). Intellisense continue à me donner une erreur.Avoir de la difficulté à trouver ce que Html.CheckboxFor() veut comme une expression LINQ

Voilà mes modèles de vue:

public class PlatformListing 
{ 
    public Platform Platform { get; set; } 
    public bool IsSelected { get; set; } 
} 

public class AdminGameReviewViewModel 
{ 
    public Game GameData { get; set; } 
    public List<Genre> AllGenres { get; set; } 
    public List<PlatformListing> AllPlatforms { get; set; } 
} 

et mon (très probablement horribles) code du contrôleur qui renseigne le AdminGameReviewViewModel et l'envoie à la vue:

public ActionResult EditReview(int id) 
    { 
     var game = _siteDB.Games.Include("Genre").Include("Platforms").Include("Content").Single(g => g.GameID == id); 
     var genres = _siteDB.Genres.OrderBy(g => g.Name).ToList(); 
     var platforms = _siteDB.Platforms.OrderBy(p => p.Name).ToList(); 
     List<PlatformListing> platformListings = new List<PlatformListing>(); 

     foreach (Platform platform in platforms) 
     { 
      platformListings.Add(new PlatformListing { Platform = platform, IsSelected = game.Platforms.Any(p => p.PlatformID == platform.PlatformID) ? true : false }); 
     } 

     var model = new AdminGameReviewViewModel { GameData = game, AllGenres = genres, AllPlatforms = platforms }; 

     return View(model); 
    } 

Je ne suis pas sûr de ce que Il me manque, et ça me rend fou. La documentation que j'ai trouvée n'a pas vraiment éclairci la question non plus.

EDIT: Code de vue pertinent (vue partielle à utiliser à la fois Créer et modifier) ​​-

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<HandiGamer.ViewModels.AdminGameReviewViewModel>" %> 

<p> 
    <%: Html.Label("Game Title") %> 
    <%: Html.TextBoxFor(model => model.GameData.GameTitle) %> 
    <%: Html.ValidationMessageFor(model => model.GameData.GameTitle) %> 
</p> 
<p> 
    <%: Html.LabelFor(model => model.GameData.Genre) %> 
    <%: Html.DropDownList("Genre", new SelectList(Model.AllGenres, "GenreID", "Name", Model.GameData.GenreID)) %> 
</p> 
<p> 
    <%: Html.Label("Platforms") %><br /> 
    <% foreach(var item in Model.AllPlatforms) { %> 
     <%: item.Platform.Name %> <%: Html.CheckBox("Platforms[]", item.IsSelected, new { id = item.Platform.PlatformID }) %><br /> 
    <% } %> 
</p> 
<p> 
    <%: Html.Label("Review Title") %> 
    <%: Html.TextBoxFor(model => model.GameData.Content.Title) %> 
</p> 
<p> 
    <%: Html.Label("Review") %> 
    <%: Html.TextAreaFor(model => model.GameData.Content.Text) %> 
</p> 
<p> 
    <%: Html.Label("Review Score") %> 
    <%: Html.DropDownList("Score", new SelectList(new int[] {1, 2, 3, 4, 5}, "ReviewScore")) %> 
</p> 
<p> 
    <%: Html.LabelFor(model => model.GameData.Pros) %><br /> 
    <%: Html.TextBox("Pros[]") %><br /> 
    <%: Html.TextBox("Pros[]") %><br /> 
    <%: Html.TextBox("Pros[]") %><br /> 
    <%: Html.TextBox("Pros[]") %><br /> 
    <%: Html.TextBox("Pros[]") %> 
</p> 

Répondre

3

Je crois que vous voulez quelque chose comme:

Html.CheckBoxFor(model => model.AdminGameReviewViewModel[i].IsSelected) 

au sein de certaines boucle qui est définie i dans ta vue. La publication de votre vue peut aider à la clarifier.