lundi 19 septembre 2016

Passing View collection to Partial View

I am trying to pass part of a model to a Partial view. So in this example I just want to pass student information. I have the following POCO classes.

public class Student
{
    public int studentID { get; set; }
    public string name { get; set; }
}

public class Teacher
{
    public Teacher()
    {
        students = new List<Student>();
    }
    public int id { get; set; }
    public List<Student> students { get; set; }
}

Then in my view something like this which I was hoping would pass the contents of my collection 'Model.students' into my partial view so I can this process it. Build a table.

@model Project.Models.Teacher
<div id="MainGridContainer">
    @{Html.RenderPartial("_Student", Model.students );}
</div>

So my partial view does not pick up the students collection and process it.

  @model IList<Project.Models.students>
  <table>
  @for (int i = 0; i < Model.Count(); i++)
  {
    <tr>
    <td>@Model[i].name</td>
    <td>@Model[i].studentID</td>
    </tr>
  }
  </table>

I must have the wrong idea with what to do here and wondered for any advise on this. Thank you.




Aucun commentaire:

Enregistrer un commentaire