【问题标题】:System.Collections.Generic.IEnumerable does not contain a definition for ErrorSystem.Collections.Generic.IEnumerable 不包含错误的定义
【发布时间】: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&lt;CreateStudent&gt; 并仅添加 1 个 CreateStudent - 为什么不将 CreateStudent 设置为模型 - return View(model) 然后 @model acgs_qm.Models.CreateStudent。这将解决错误

标签: c# asp.net asp.net-mvc list ienumerable


【解决方案1】:

如果您的视图旨在仅创建一个学生记录,则传入一个学生,而不是只有一行的“列表”。

将您的 @model 更改为:

@model acgs_qm.Models.CreateStudent

还有你的控制器操作:

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

【讨论】:

    【解决方案2】:

    如果你想在列表中显示所有学生,你必须遍历它,例如

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

    【讨论】:

      猜你喜欢
      • 2012-01-16
      • 1970-01-01
      • 2013-03-26
      • 2018-12-25
      • 1970-01-01
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多