2013-07-29 54 views
0

這裏是CourseRegisterModel的定義:foreach語句不能對變量MVC3錯誤操作

public class CourseRegisterModel 
{ 
    public StudentModel Student { get; set; } 

    public CourseModel Course { get; set; } 

    public CourseModel Course1 { get; set; } 
    public CourseModel Course2 { get; set; } 
    public CourseModel Course3 { get; set; } 
    public CourseModel Course4 { get; set; } 
    public CourseModel Course5 { get; set; } 

    public List<SelectListItem> CoursesList { get; set; } 

    public DateTime RegisterDate { get; set; } 

} 

這裏是我的控制器功能,其執行上的主頁加載:

public ActionResult Home() 
    { 
     //Retrieve all registered courses for this bloody student 

     ServiceCourseClient client = new ServiceCourseClient(); 
     Course[] coursespending; 
     var loggedInRollNumber = Request.Cookies["RollNumber"].Value; 
     coursespending = client.GetPendingRegisteredCourses(loggedInRollNumber.ToString()); 
     List<CourseRegisterModel> coursesRegisteredmodelList = new List<CourseRegisterModel>(); 
     CourseRegisterModel CRM = null; 
     int i = 0; 
     foreach (var serviceCourseRegistered in coursespending) 
     { 
      CRM = new CourseRegisterModel(); 
      if (i == 0) 
      { 
       CRM.Course1.Code = serviceCourseRegistered.Code; 
       CRM.Course1.Name = serviceCourseRegistered.Name; 
      } 
      else if (i == 1) 
      { 
       CRM.Course2.Code = serviceCourseRegistered.Code; 
       CRM.Course2.Name = serviceCourseRegistered.Name; 
      } 
      else if (i == 2) 
      { 
       CRM.Course3.Code = serviceCourseRegistered.Code; 
       CRM.Course3.Name = serviceCourseRegistered.Name; 
      } 
      else if (i == 3) 
      { 
       CRM.Course4.Code = serviceCourseRegistered.Code; 
       CRM.Course4.Name = serviceCourseRegistered.Name; 
      } 
      else if (i == 4) 
      { 
       CRM.Course5.Code = serviceCourseRegistered.Code; 
       CRM.Course5.Name = serviceCourseRegistered.Name; 
      } 
      i++; 
      coursesRegisteredmodelList.Add(CRM); 
     } 

     return View(coursesRegisteredmodelList); 
    } 

這裏是觀我試圖顯示註冊課程:

@model StudentRegistrationPortal.Models.CourseRegisterModel 
@{ 
    ViewBag.Title = "Welcome Student"; 
} 

<h2>Welcome 
@Context.User.Identity.Name 
</h2> 
@Html.ActionLink("[Sign Out]", "SignOut", "Student") 
<ul> 
<li>@Html.ActionLink("Register Courses", "registerCourses", "Course")</li> 
</ul> 

<table> 
<tr> 
    <th> 
     RollNumber 
    </th> 
    <th> 
     Course Code 
    </th> 
    <th> 
     Course Name 
    </th> 
</tr> 

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Context.User.Identity.Name 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Name) 
    </td> 
</tr> 
} 

</table> 

但是foreach循環在上面視圖正在給出以下編譯時錯誤: foreach statement cannot operate on variables type CourseRegistrationModel beacuse it does not contain a public definition for GetEnumerator

請幫忙。

+0

這是奇怪的,因爲您應該得到一個錯誤,即您的模型類型與傳入的模型不匹配,因爲您傳遞了CoureseRegistrationModel的列表,但您的視圖只有一種類型。 –

回答

6

你的模型需要一個IEnumerable。見下文。

@model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel> 
@{ 
    ViewBag.Title = "Welcome Student"; 
} 

<h2>Welcome 
@Context.User.Identity.Name 
</h2> 
@Html.ActionLink("[Sign Out]", "SignOut", "Student") 
<ul> 
<li>@Html.ActionLink("Register Courses", "registerCourses", "Course")</li> 
</ul> 

<table> 
<tr> 
    <th> 
     RollNumber 
    </th> 
    <th> 
     Course Code 
    </th> 
    <th> 
     Course Name 
    </th> 
</tr> 

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Context.User.Identity.Name 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.Name) 
    </td> 
</tr> 
} 

</table> 

你試圖做

foreach(CourseRegisterModel item in CourseRegisterModel) 
{ 
    ... 
} 

這是不可能的,你想要的。

foreach(CourseRegisterModel item in IEnumerable<CourseRegisterModel>) 
{ 
    ... 
} 
0

您需要將您的型號設爲ListIEnumerable。您只需向下傳遞不落實GetEnumerator類,它應該是:

@model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel> 

或者:

@model List<StudentRegistrationPortal.Models.CourseRegisterModel> 
0

您的視圖模型是如下所示的單個對象:

@model StudentRegistrationPortal.Models.CourseRegisterModel 

你可能會想遍歷CoursesList財產在你的foreach循環:

@foreach (var item in Model.CoursesList) { .... } 
相關問題