-2

Possible Duplicate:
ASP.NET mvc : populate (bind data) in listbox

this is what I have: I have a database with a table named "Students". I also have a partial class "Students". I need to show to listboxes to the users: one with normal students and one with students who haven't got a class. So I have one actionresult returning two listboxes... but it's not working... what do I do wrong?

This is the layout of the partial class "Docent":

public IEnumerable<Student> GiveStudentsNormal(int lesson, int klass)
        {
            Lesson l = giveLesson(lesson);
            Klass k = GiveKlass(lesson, klass);
            return (k.Students).AsEnumerable();
        }


public IEnumerable<Student> GiveStudentsNoClass(int lesson, int klass)
            {
                Lesson l = giveLesson(lesson);
                Klass k = GiveKlass(lesson, klass);
                return (k.Students).AsEnumerable();
            }

So in my controller I use those students lists to populate my two listboxes. So one controller needs to populate those two lisboxes who are in the same view. But my question is, can I pass the object "Student" to my listbox? Or should I convert everything to strings?

Class StudentModel:

public class StudentModel
    {
        public IEnumerable<String> NormalStudents { get; set; }
        public IEnumerable<String> NoClassStudents { get; set; }
    }

But do I need the class StudentModel actually? Controller:

public ActionResult IndexStudents(Docent docent, int lessonid, int klassid)
        {
            var studentModel = new StudentModel
            {
               NormalStudents = docent.GiveStudentsNormal(lessonid, classid),
               NoClassStudents = docent.GiveStudentsNoClass(lessonid, classid)
            };

            return View(studentModel);
        }

View:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

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

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

</asp:Content>

The view also returns an error: it doesn't know the methods NormalStudents and NoClassStudents

What do I have to do?

0

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.