System.Collections.Generic.IEnumerable 不包含错误的定义

System.Collections.Generic.IEnumerable does not contain a definition for Error

我需要将列表传递给 IEnumerable 视图,但出现此错误:

System.Collections.Generic.IEnumerable does not contain a definition for si_id and no extension method si_id accepting a first argument of type System.Collections.Generic.IEnumerable could be found (are you missing a using directive or an assembly reference?)

查看

            @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);
}

我无法更改 IEnumerable,因为我正在像这样传递 post 的值

[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);

如果您的视图打算只创建一个学生记录,则传入一个学生,而不是只有一行的 'list of'。

将您的 @model 更改为:

@model acgs_qm.Models.CreateStudent

以及您的控制器操作:

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

如果您想显示列表中的所有学生,您必须对其进行循环,例如

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