2012-02-08 41 views
0

當IAM裝盤,以填補例如中間表時,學生要添加coourse我有我的觀點一個DropDownList的seletec學生一個DropDownList的所有課程和在我看來DROPDOWNLIST所有學生如何設置atributes爲@ Html.DropDownList

table sample 
{ 
id, 
studentid, 
courseid, 
grade, 
studentTeacher 
} 

我控制器

ViewBag.Student= new SelectList(db.Course.Where(c => c.StudentId== id), "StudentId","FullName"); 
ViewBag.CourseId = new SelectList(db.Course, "CourseId ", "Name"); 
ViewBag.StudentId = new SelectList(db.Student, "StudentId","FullName"); 

@Html.DropDownList("Student") 
@Html.DropDownList("CourseId ") 
@Html.DropDownList("StudentId ", string.Empty) 

我得到一個錯誤它說沒有類型'IEnumerable'有關鍵'學生'的ViewData項目。

,所以我guees正在尋找一個關鍵StudentId,所以待會我怎麼能告訴DropDownList的看向從(學生),其鍵(StudentId)

回答

0

有沒有ViewData的項目鍵入'IEnumerable',鍵爲'Student'。

這意味着你還沒有提供任何SelectListItem集合(讓我們說任何數據)的下拉列表。 ASP.NET MVC自動嘗試使用與指定屬性相同的鍵在ViewData集合中查找數據。在你的情況下,它是'學生',因爲你已經提供了這樣的屬性名稱爲@Html.DropDownList("Student")

嘗試將數據設置爲ViewData["Student"]集合,或直接來填充它:

@Html.DropDownList("Student", ViewBag.Student) 
// etc.