ASP.NET MVC:在列表框中填充(绑定数据)

4

我需要从数据库中获取数据并将其填充到两个列表框中。 但是我需要在一个视图中显示两个列表框,因此我创建了一个ListBoxModel类。

public class ListboxModel
{
    public ListBox LB1 { get; set; }
    public ListBox LB2 { get; set; }
}

我的控制器中:

public ActionResult IndexStudents(Docent docent, int lessonid, int classid)
    {
        var MyListBox = new ListboxModel
        {
            LB1 = new ListBox(docent.ReturnStudentsNormal(lessonid, classid),
            LB2 = new ListBox(docent.ReturnStudentsNoClass(lessonid, classid)
        };
        return View(MyListBox);
    }

但是这段代码不起作用,我该如何将数据绑定到列表框?所以我想使用两个模型,一个用于每个列表框......一个用于正常学生和一个用于未订阅课程的学生。我该怎么做?在我的视图中需要编写什么代码?类似以下内容:

<div class="editor-field">
     <%: Html.ListBox("IndexStudentsNormal", Model.LB1) %> 

<div class="editor-field">
      <%: Html.ListBox("IndexStudentsNoClass", Model.LB2) %> 

列表框必须是具有多列的列表框,以便可以包含学生的姓名、姓氏、班级和老师等信息。

学生是一个对象,我想在列表框中显示学生的姓名、姓氏、班级等信息。

那么我能在ListBox中使用一个对象吗,还是必须将所有内容转换为字符串?

如何实现呢?

谢谢!

2个回答

4

模型不应包含ListBox,它只应包含学生列表。关键在于尽可能地简化模型,它实际上只应该是一组属性的getter和setter。视图负责将模型的属性绑定到HTML元素。

模型:

public class StudentModel
{
  public IList<string> NormalStudents {get;set;}
  public IList<string> NoClassStudents {get;set;}
}

控制器:

public ActionResult IndexStudents(Docent docent, int lessonid, int classid)
{
    var studentModel = new ListboxModel
    {
       NormalStudents = docent.ReturnStudentsNormal(lessonid, classid),
       NoClassStudents = docent.ReturnStudentsNoClass(lessonid, classid)
    };

    return View(studentModel);
}

查看:

<div class="editor-field">
    <%: Html.ListBox("IndexStudentsNormal", Model.NormalStudents) %>
  </div>

  <div class="editor-field">
    <%: Html.ListBox("IndexStudentsNoClass", Model.NoClassStudents) %>
  </div>

它不起作用:它给了我以下错误: 错误2:'object'不包含名称为'NormalStudents'的定义,也没有接受类型为'object'的第一个参数的扩展方法'NormalStudents'(您是否缺少使用指令或程序集引用?) - Lorenzo
你在视图中正确地设置了模型吗? - Jason

2
根据Jason的回答,你的视图文件中第一行应该包含以下内容:(原文链接)
<%@ Inherits="System.Web.Mvc.ViewPage<StudentModel>" %>

这条语句告诉视图,“Model”是“StudentModel”类型。如果此行中还有其他部分(如Title、Language、MasterPageFile等),它们可以保留。


-- 编辑:添加长注释 --

需要记住的是,SelectListItem有三个必需的部分: Value、Text和Selected。Value是键,例如StudentId或DocentId。Text在列表中显示,例如StudentName或DocentName。Selected指示该项是否在列表中选择,通常为false。

目前,您只有返回学生姓名列表的方法(Docent.ReturnStudentsNormal()和Docent.ReturnStudentsNoClass())。我建议将这些方法改为返回键值对列表,键为StudentId,值为StudentName。

然后,您可以更改模型类:

public class StudentModel
{
  List<SelectListItem> NormalStudents;
  List<SelectListItem> StudentsNoClass;
}

在你的控制器中

public ActionResult IndexStudents(Docent docent, int lessonId, int classId)
{
  var studentModel = new StudentModel();

  var normalStudents = docent.ReturnStudentsNormal(lessonId, classId);
  foreach (var student in normalStudents)
  {
    studentModel.NormalStudents.Add(new SelectListItem() {Value = student.Key, Text = student.Value});
  }

  var studentsNoClass = docent.ReturnStudentsNormal(lessonId, classId);
  foreach (var student in studentsNoClass)
  {
    studentModel.StudentsNoClass.Add(new SelectListItem() {Value = student.Key, Text = student.Value});
  }

  return View(studentModel);
}

现在,您将能够直接在您的模型上使用这些属性来使用Html.ListBox()

好的,我已经更改了我的视图中的第一行,现在它能够识别我的方法了!谢谢!但是出现了另一个问题: "Html.Listbox" 有一些无效的参数... 我该如何将 IEnumerable<String> 转换为 IEnumerable<SelectListItem>?-------------------------------------------------错误3 参数3:无法从 'System.Collections.Generic.IEnumerable<string>' 转换为 'System.Collections.Generic.IEnumerable<System.Web.Mvc.SelectListItem>'。 - Lorenzo
我应该定义一个返回SelectListItems的方法吗? 例如:public IEnumerable<SelectListItem> optionsList { get { //代码 } } - Lorenzo
哦,我记得在这里添加了一条评论。无论如何,我更新了我的答案,并提供了一种建议的方法来组合SelectListItems列表以供视图使用。 - Thorin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接