ASP.NET mvc:在列表框中填充(绑定数据) [英] ASP.NET mvc : populate (bind data) in listbox

查看:21
本文介绍了ASP.NET mvc:在列表框中填充(绑定数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用数据库中的数据填充两个列表框.但是我需要在一个视图中显示两个列表框,所以我创建了一个 ListBoxModel 类.

公共类ListboxModel{公共列表框 LB1 { 获取;放;}公共列表框 LB2 { 获取;放;}}

在我的控制器中:

public ActionResult IndexStudents(Docent docent, int classid, int classid){var MyListBox = 新的 ListboxModel{LB1 = new ListBox(docent.ReturnStudentsNormal(lessonid, classid),LB2 = new ListBox(docent.ReturnStudentsNoClass(lessonid, classid)};返回视图(MyListBox);}

但是这段代码不起作用,我如何将数据绑定到列表框?所以我想使用 2 种模型,一种用于每个列表框……一种用于普通学生,另一种用于未订阅课程的学生.我怎么能那样做?在我看来,我必须编写哪些代码?类似的东西:

<%: Html.ListBox("IndexStudentsNormal", Model.LB1)%>

<%: Html.ListBox("IndexStudentsNoClass", Model.LB2)%>

列表框必须是具有多个列的列表框,以便它可以包含学生的姓名、老师姓名、班级、老师.

Student 是一个对象,我想在该列表框中显示 student.Name、student.Sirname、student.Class 等.

那么我可以在 ListBox 中使用对象,还是必须将所有内容都转换为字符串?

我该怎么做?

提前致谢!

解决方案

基于 Jason 的回答,您视图中的第一行应包括:

<%@ Inherits="System.Web.Mvc.ViewPage"%>

这告诉您的视图模型"是 StudentModel 类型.如果第一行中还有其他位(标题、语言、MasterPageFile 等),它们可以留在那里.

<小时>

-- edit:添加较长的评论--

需要记住的是,SelectListItem 具有三个必需的部分:Value、Text 和 Selected.值是关键,所以像 StudentId 或 DocentId 这样的东西.文本显示在列表中,例如 StudentName 或 DocentName.Selected 表示该项目是否在列表中被选中,一般为false.

现在看起来您的方法只返回学生姓名列表(Docent.ReturnStudentsNormal() 和 Docent.ReturnStudentsNoClass()).我会让这些方法返回一个键值对列表,键是 StudentId,值是 StudentName.

然后您可以将模型类更改为

公共类StudentModel{列表普通学生;列表学生无课;}

并在您的控制器中

public ActionResult IndexStudents(Docent docent, int courseId, 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 studentNoClass = docent.ReturnStudentsNormal(lessonId, classId);foreach (var student in studentNoClass){studentModel.StudentsNoClass.Add(new SelectListItem() {Value = student.Key, Text = student.Value});}返回视图(学生模型);}

现在您可以直接在模型上为 Html.ListBox() 使用这些属性.

I need to populate two listboxes with data from a database. But I need to display two listboxes in one view, so I created a ListBoxModel class.

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

And in my Controller:

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

But this code does not work, how can I bind the data to the listboxes? So I'd like to use 2 models, one for each listbox... one with normal students and one with students who are not subscribed for lessons. How could I do that? And what code do I have to write in my view? Something like:

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

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

The listbox has to be a listbox with multiple colums so that it can contain the name, sirname, class, teacher of the student.

Student is an object, I'd like to display student.Name, student.Sirname, student.Class, and so on in that listbox.

So can I use an object in a ListBox, or do I have to convert everything to strings?

How can I do that?

Thanks in advance!

解决方案

Based on Jason's answer, the first line in your view should include:

<%@ Inherits="System.Web.Mvc.ViewPage<StudentModel>" %>

This tells your view that "Model" is of type StudentModel. If there's other bits in this first line (Title, Language, MasterPageFile, etc), they're fine to stay there.


-- edit: add longish comments --

The thing to remember is that a SelectListItem has three required parts: Value, Text, and Selected. Value is the key, so something like StudentId or DocentId. Text is displayed in the list, so something like StudentName or DocentName. Selected indicates whether this item is selected in the list, typically false.

Right now it looks like you have methods that only return a list of the student names (Docent.ReturnStudentsNormal() and Docent.ReturnStudentsNoClass()). I would have these methods return a list of key-value pairs, key being StudentId and value being StudentName.

Then you can change your model class to be

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

and in your controller

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

Now you'll be able to use these properties on your model directly for Html.ListBox().

这篇关于ASP.NET mvc:在列表框中填充(绑定数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆