モデル
public class Student
{
public int StudentId { get; set; }
public string StudentName{ get; set; }
//FYI..Its virtual because of EF relationship
public virtual ICollection<Class> Classes{ get; set; }
}
public class Class
{
public int ClassId { get; set; }
public string ClassName{ get; set; }
}
ビュー - EditStudent
@model Student
@Html.TextBoxFor(m => m.StudentName)
//I get the error for following..see below
@Html.EditorFor(m => m.Classes);
学生/ EditorTemplates /クラス
@model Class
<div>
@*checkbox here*@
@Html.LabelFor(x => x.ClassName)
</div>
コントローラHtml.EditorFor @文のビューで
public ActionResult EditStudent(int id)
{
ViewBag.Classes = repository.GetClasses();
Student student = repository.GetStudent(id);
return View("EditStudent", student);
}
エラー
(M => m.Classes);モデルアイテムは、 'System.Collections.Generic.HashSet`1 [Class]'のタイプですが、この辞書 には 'Class'タイプのモデルアイテムが必要です。
基本的に、私が達成しようとしていることは(私はまだコードのその部分に達していない)、それの横にあるチェックボックスで利用可能なすべてのクラスのリストを表示することです。次に、生徒のすべてのクラスが登録されていることを確認し、選択を変更できるようにします。
- 特定のモデルでチェックボックスのリストを表示するにはどうすればよいですか。
- EditorTemplateをViewBag.Classes(どのように)にバインドする必要がありますか?
- Post ActionMethodでチェックボックスの値を選択する必要があります。
- CheckBoxListHelperを作成するよう提案されている記事をいくつか読んでいますが、単純なリストを表示する必要があるため、EditorTemplateで行うことができます。
お勧めします。ありがとう。