我需要将列表传递给IEnumerable
视图,但出现了以下错误:
System.Collections.Generic.IEnumerable不包含siid的定义,也找不到接受System.Collections.Generic.IEnumerable类型的第一个参数的扩展方法siid(是否缺少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);
}
我不能更改IEnumerable
,因为我像这个一样传递我帖子的值
[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);
如果您的视图只想创建一个学生记录,那么传入一个学生,而不是只有一行的"列表"。
将您的@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 ... *@
}