2015-12-02 75 views
0

我需要一個列表傳遞給IEnumerable視圖的定義,但這個錯誤出來:System.Collections.Generic.IEnumerable不包含錯誤

System.Collections.Generic.IEnumerable不包含定義和SI_ID沒有擴展方法SI_ID接受型System.Collections.Generic.IEnumerable的第一個參數可以找到(是否缺少using指令或程序集引用?)

查看

  @model IEnumerable<acgs_qm.Models.CreateStudent> 

      <div class="editor-label"> 
       @Html.LabelFor(model => model.si_id) //Error 
      </div> 
      <div class="editor-field"> 
       @Html.TextBoxFor(model => model.si_id, new { @readonly = "readonly" }) 
       @Html.ValidationMessageFor(model => model.si_id) 
      </div> 


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

控制器

[HttpGet] 
public ActionResult RegisterStudent() 
{ 
    Models.ModelActions action = new Models.ModelActions(); 
    var model = new acgs_qm.Models.CreateStudent 
    { 
     GradeLevel = action.getGrade(), 
     Guardian = action.getGuardian(), 
     si_id = action.getcode("student_info_tb", "si_id", "1") 
    }; 
    List<CreateStudent> stud = new List<CreateStudent>(); 
    stud.Add(model); 
    return View(stud); 
} 

我不能,因爲我通過我的文章的價值這樣

[HttpPost] 
public ActionResult RegisterStudent(CreateStudent Create, string submitbutton, string searchTerm) 
{ 

    acgs_qm.Models.ModelActions Ma = new acgs_qm.Models.ModelActions(); 
    List<CreateStudent> stud = new List<CreateStudent>(); 

    switch (submitbutton) 
    { 
     case "search": 
      stud = Ma.searchStud(searchTerm); 

      return View(stud); 
+0

如果你做模型[0] .si_id會發生什麼? – VictorySaber

+4

你假裝它是一個單一的項目,但實際上它是一個列表。 –

+0

@PatrickHofman我該如何解決這個問題? – Renz

回答

2

如果你的觀點是爲了創建只是一個改變IEnumerable單一的學生記錄,然後通過一名學生,而不是隻有一行的「清單」。

更改@model到:

@model acgs_qm.Models.CreateStudent 

而且你的控制器動作:

return View(model); // model is a single student record 
1

我要在列表,則必須遍歷它,例如顯示所有學生

@model IEnumerable<acgs_qm.Models.CreateStudent>  
@foreach (var student in Model) { 
     <div class="editor-label"> 
       @Html.LabelFor(m => student.si_id) 
     </div> 
     @* etc ... *@ 
    }