2012-09-29 8 views
10

私は多くを検索し、検索や別のテクニック(stackoverflowなど)を試して3日間過ごしていますが、asp.net mvcにチェックボックスリストを実装するための解決策はありません。最後に私は自分の問題をstackoverflowに投稿しています。
私のモデルはこのように見えます。私のモデル(1つのカテゴリは、多くのプロジェクトが含まれていてもよいし、プロジェクトは多くのカテゴリに属している場合があります)
私のコントローラの多くの関係に多くのAsp.Net MVC4表示チェックボックスリスト

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

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

私の見解。

@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> 

だから、私の質問は、は私が私の見解でカテゴリについてのCheckBoxListを表示するにはどうすればよいのですか?
このチェックボックスリストから選択した値を取得するにはどうすればよいですか?

答えて

17

あなたがたとえば、あなたがこれを行うことができ、すべてのカテゴリのリストを持っていますオブジェクトを持っている必要があります:あなたのビューの上部にある

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

    return View(); 
} 

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

これを交換してください。

<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> 

上記@balexandreによって提案されたこの

<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> 

戻ってあなたのコントローラで

[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は私にあるもう一つのことを、伝えることができますどのように各カテゴリ 'foreachの(カテゴリのint猫)と –

+0

@DotNetDreamer db.InsertCategory(model.ProductId、猫)を介して、Iループ;'とあなた両方の値を保持するテーブル、 'ProductId'と' CategoryId'のみを含む 'ProductCategories'テーブルが必要です。 EDITINGの場合は、新しい製品のProductCategoriesテーブルでその製品のすべてのカテゴリを削除して、それらを再度追加する必要があります。 – balexandre

+0

はい、すでにI-e ProjectCategoriesを行っていますが、これを3番目のテーブルに挿入する必要がありますか? –

4

回答が素晴らしい作品のために。しかし、私はCheckBoxListのために次のHTML拡張を使用しました。

1. CheckboxList in ASP.net MVC4
2. Source Code:HTML Extension Helper method (Github)

このヘルパーメソッドを使用しての主な利点は、クリーンなコードと読みやすくなります。例えば。

@Html.CheckBoxListFor(model => model.SelectedItems, Model.AllItems) 
+0

私は上記の質問と回答の両方をアップアップしました。 – Rohit