【发布时间】:2015-12-02 13:35:07
【问题描述】:
我需要将列表传递给IEnumerable 视图,但出现此错误:
System.Collections.Generic.IEnumerable 不包含 si_id 的定义,并且找不到接受 System.Collections.Generic.IEnumerable 类型的第一个参数的扩展方法 si_id(您是否缺少 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[0].si_id 会发生什么?
-
你假装它是一个单项,但实际上它是一个列表。
-
@PatrickHofman 先生,我该如何解决这个问题?
-
您正在创建一个
List<CreateStudent>并仅添加 1 个CreateStudent- 为什么不将CreateStudent设置为模型 -return View(model)然后@model acgs_qm.Models.CreateStudent。这将解决错误
标签: c# asp.net asp.net-mvc list ienumerable