1

下面是 CourseRegisterModel 的定义:

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

请帮忙。

4

3 回答 3

7

您的模型必须是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>)
{
    ...
}
于 2013-07-29T15:37:32.523 回答
1

您需要使您的模型 aListIEnumerable. 您只需传递未实现的类GetEnumerator,它应该是:

@model IEnumerable<StudentRegistrationPortal.Models.CourseRegisterModel>

或者:

@model List<StudentRegistrationPortal.Models.CourseRegisterModel>
于 2013-07-29T15:37:17.487 回答
0

您的视图模型是单个对象,如下所示:

@model StudentRegistrationPortal.Models.CourseRegisterModel

您可能想要在 foreach 循环中迭代 CoursesList 属性:

@foreach (var item in Model.CoursesList) { .... }
于 2013-07-29T15:39:45.720 回答