Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

c# - ASP.NET mvc : populate (bind data) in 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The model shouldn't contain a ListBox, it should just contain the lists of students. The trick is to keep the Model as simple as possible, it should really only be a bunch of property getters and setters. The view is responsible for binding a Model's properties to HTML elements.

Model:

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

Controller:

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

View:

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...