Hasan A Yousef Hasan A Yousef - 1 month ago 19
C# Question

C# Razor model and passing

How can I transfer data/model between partial/views/controllers in C# MVC?

I'm trying to duplicate this example, but the

@model IEnumerable<MVC_BasicTutorials.Models.Student>
is given me an error, with red line under the
Model


_StudentList Partial.cshtml


@model IEnumerable<MVC_BasicTutorials.Models.Student>

<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.StudentName)
</th>
<th>
@Html.DisplayNameFor(model => model.Age)
</th>
<th></th>
</tr>

@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.StudentName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Age)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.StudentId }) |
@Html.ActionLink("Details", "Details", new { id=item.StudentId }) |
@Html.ActionLink("Delete", "Delete", new { id = item.StudentId })
</td>
</tr>
}

</table>


StudentController
:

public class StudentController : Controller
{
public class Student
{
public int StudentId { get; set; }
public string StudentName { get; set; }
public int Age { get; set; }
}
IList<Student> students;

public StudentController()
{
students = new List<Student>{
new Student() { StudentId = 1, StudentName = "John", Age = 18 } ,
new Student() { StudentId = 2, StudentName = "Steve", Age = 21 } ,
new Student() { StudentId = 3, StudentName = "Bill", Age = 25 } ,
new Student() { StudentId = 4, StudentName = "Ram" , Age = 20 } ,
new Student() { StudentId = 5, StudentName = "Ron" , Age = 31 } ,
new Student() { StudentId = 6, StudentName = "Chris", Age = 17 } ,
new Student() { StudentId = 7, StudentName = "Rob",Age = 19 } ,
};

}
// GET: Student
public ActionResult Index()
{

return View(students);
}
}


Index.cshtml
:

@model IEnumerable<MVC_BasicTutorials.Models.Student>

<h3>Student List</h3>
<p>
@Html.ActionLink("Create New", "Create")
</p>

@{
Html.RenderPartial("_StudentList", Model);
}


I read this http://stackoverflow.com/a/11326470/2441637 but could not get it solved :( if I run the app, I get an error at
@foreach (var item in Model)
that is
NullReferenceException: Object reference not set to an instance of an object.

Answer

Your Student class is defined as a sub class of StudentController. So you need to use the proper namespace / fully qualified class name when using this class as the model type.

Change the first line to

@model IEnumerable<PutYourNameSpaceHere.StudentController.Student>

You need to replace PutYourNameSpaceHere with the namespace under your StudentController class is

Or

Move the Student class to a new file (or even existing), but under the namespace MVC_BasicTutorials.Models

namespace MVC_BasicTutorials.Models
{
    public class Student
    {
       public int StudentId { get; set; }
       public string StudentName { get; set; }
       public int Age { get; set; }
    }
}

When doing this, you need to make sure to have a using statement in your StudentController so that you can use this class

So add this as the first line of the StudentController class

@using MVC_BasicTutorials.Models
Comments