2012-01-17 7 views
-2

可能性の重複:
CheckboxList in MVC3.0MVC3 CheckBoxListの

こんにちは、私は二つのクラスの本や著者を持っていると私はしたいが、あなたが本を追加するとき、著者のリストを表示され、選択することができるということです1つ以上の1つ以上。これを行う最善の方法はチェックリストですが、mvc3でこれを行う方法は見つけられません。しかし、私はあなたが知っているために、あなたの著者にSelectedブール型プロパティを追加することができ、これらの私は大いに

public class Book 
{ 
    public int IdBook {get; set;} 
    public string Title {get; set;} 
    public List<author> Authors {get; set;} 
} 


public class Author 
{ 
    public int IdAuthor{get; set;} 
    public string Name {get; set;} 
} 
+1

この質問を見るhttp://stackoverflow.com/questions/4872192/checkboxlist-in-mvc3-0 – AyKarsi

答えて

1

をいただければ幸い作ることができるか、誰かが私を伝えることができますので、もし、私はMVCで始まる理解いくつかの例を読んでいませんその後、

public class Author 
{ 
    public int IdAuthor { get; set; } 
    public bool Selected { get; set; } 
    public string Name { get; set; } 
} 

と::

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     var book = new Book 
     { 
      IdBook = 1, 
      Title = "foo bar", 
      Authors = new[] 
      { 
       new Author { IdAuthor = 1, Name = "author 1" }, 
       new Author { IdAuthor = 2, Name = "author 2" }, 
       new Author { IdAuthor = 3, Name = "author 3" }, 
      }.ToList() 
     }; 
     return View(book); 
    } 

    [HttpPost] 
    public ActionResult Index(Book book) 
    { 
     return View(book); 
    } 
} 

とビューで:彼は与えられた書籍やないために選択したかどうかを

@model Book 

@using (Html.BeginForm()) 
{ 
    <div> 
     @Html.LabelFor(x => x.Title) 
     @Html.EditorFor(x => x.Title) 
    </div> 

    for (int i = 0; i < Model.Authors.Count; i++) 
    { 
     @Html.CheckBoxFor(x => x.Authors[i].Selected) 
     @Html.LabelFor(x => x.Authors[i].Selected, Model.Authors[i].Name)  
     @Html.HiddenFor(x => x.Authors[i].Name) 
    } 

    <p> 
     <button type="submit">OK</button> 
    </p> 
} 
0

あなたが(それはあなたがメタデータのこの種を保存することができ、または、モデル - ビュークラスを作成します)あなたの著者モデル - ビュークラスにbool Selectedフィールドを追加する必要がありますし、それが結合だけで普通のことです通常のように:

@foreach(var book in Model.Books) 
{ 
    <table> 
    <!-- fill in the book details and create the checkbox list template --> 

    @foreach(var author in book.Authors) 
    { 
     <tr><td> 
     @Html.CheckBoxFor(x=>x.Selected); <!-- the checkbox --> 
     </td><td> 
     @Html.DisplayFor(x=>x);   <!-- the description --> 
              <!-- could also go with a nice partial view here --> 
     </td></tr> 
    } 
    </table> 
} 
関連する問題