2012-09-29 13 views
10

Ich habe viel gesucht und verbringen 3 Tage nur zum Suchen und Ausprobieren verschiedener Technik (auf Stackoverflow etc), aber ich finde keine Lösung für die Implementierung Checkboxlist in asp.net mvc. Und zum Schluss poste ich mein Problem auf stackoverflow;
So sieht mein Modell so aus;Asp.Net MVC4 Anzeige CheckboxList

Many to Many relationship of my model(1 category may contain many project and a project may belongs to many categories) rel

viele zu viele Beziehung meines Modells (1 Kategorie können viele Projekte enthalten und ein Projekt kann zu vielen Kategorien gehört)
Mein Controller;

[HttpGet] 
    [Authorize(Roles = "Admin")] 
    public ActionResult ProjectAdd() 
    { 
     return View(); 
    } 

Meine Ansicht;

@using (Html.BeginForm()) 
{ 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Add New Project</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.ProjectHeading) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ProjectHeading) 
      @Html.ValidationMessageFor(model => model.ProjectHeading) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.ProjecctUrl) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ProjecctUrl) 
      @Html.ValidationMessageFor(model => model.ProjecctUrl) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.ProjectLongDescription) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ProjectLongDescription) 
      @Html.ValidationMessageFor(model => model.ProjectLongDescription) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.PromoFront) 
     </div> 
     @Html.EditorFor(model => model.PromoFront) 
     @Html.ValidationMessageFor(model => model.PromoFront) 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.ProjectThubmnail) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ProjectThubmnail) 
      @Html.ValidationMessageFor(model => model.ProjectThubmnail) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.ProjectImage) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.ProjectImage) 
      @Html.ValidationMessageFor(model => model.ProjectImage) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.CategoryId) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.CategoryId) 
      @Html.ValidationMessageFor(model => model.CategoryId) 
     </div> 

     <p> 
      <input type="submit" value="Create" class="submit" /> 
     </p> 

Also, meine Frage ist Wie kann ich Checkboxlist für Kategorien in meiner Ansicht angezeigt werden?
Wie bekomme ich ausgewählte Werte aus dieser Checkboxliste?

Antwort

17

Sie benötigen ein Objekt haben, die eine Liste aller Kategorien, zum Beispiel haben, können Sie dies tun:

[HttpGet] 
[Authorize(Roles = "Admin")] 
public ActionResult ProjectAdd() 
{ 
    // Get all categories and pass it into the View 
    ViewBag.Categories = db.ListAllCategories(); 

    return View(); 
} 

in der Spitze Ihrer Ansicht

@model Database.Project 
@{ 
    // retrieve the list of Categories 
    List<Database.Category> categories = ViewBag.Categories; 
} 

und dann ersetzen diese

<div class="editor-label"> 
     @Html.LabelFor(model => model.CategoryId) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.CategoryId) 
     @Html.ValidationMessageFor(model => model.CategoryId) 
    </div> 

für diesen

<div class="editor-label"> 
     <label for="categories">Categories</label> 
    </div> 
    <div class="editor-field"> 
     @foreach(var c in categories) { 

     <label class="checkbox"> 
      <input type="checkbox" name="categories" value="@c.CategoryId"> @c.CategoryName 
     </label> 

     } 
    </div> 

zurück in dem Controller

[HttpPost] 
[Authorize(Roles = "Admin")] 
public ActionResult ProjectAdd(Database.Project model, int[] categories) 
{ 
    if(ModelState.IsValid) { 

     // fill up categories 
     db.InsertAndSaveProject(model, categories); 

    } 

    ... 

    return redirectToView("ProjectAdd"); 
} 
+0

u kann mir sagen, eine Sache, die ist, wie ich Schleife durch die einzelnen Kategorien –

+0

@DotNetDreamer mit einer 'foreach (int Katze in Kategorien) db.InsertCategory (model.ProductId, Katze);' und Sie brauche eine Tabelle, die beide Werte enthält, eine 'ProductCategories'-Tabelle, die nur 'ProductId' und' CategoryId' enthält. Denken Sie daran, dass Sie zum BEARBEITEN alle Kategorien für dieses Produkt in der neuen Tabelle 'ProductCategories' löschen müssen und fügen Sie sie erneut hinzu. – balexandre

+0

ja, das habe ich schon gemacht I-e ProjectCategories aber ich muss das in die dritte Tabelle einfügen? –