View Model data in view in ASP .Net MVC
<p>In My project there are two Domain Model Classes : Student.cs and Department.cs. There is many to one relationship between the Student Class and the Department Class. </p>

<pre><code>public class Student
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int StudentId { get; set; }
    public String LastName { get; set; }
    public String FirstName { get; set; }
    public String UserName { get; set; }
    public String Password { get; set; }

    [ForeignKey("Department")]
    public int DepartmentID { get; set; }

}
</code></pre>

<p>and </p>

<pre><code> public class Department
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int DepartmentID { get; set; }

        [Required(ErrorMessage="Department Name cannot be empty")]
        public String DepartmentName { get; set; }

        [Required(ErrorMessage = "Department Code cannot be empty")]
        public String DepartmentCode { get; set; }

        public virtual ICollection&lt;Student&gt; Students { get; set; }
    }
</code></pre>

<p>Now to display the Full Name instead of the name components in View back to the end user I came up with a StudentViewModel.</p>

<pre><code>//View Model
public class StudentViewModel
{
    public Student Student { get; set; }

    public int StudentId { get; set; }

    [Display(Name="StudentName")]
    [Required(ErrorMessage="Student Name cannot be left blank")]
    public String StudentFullName
    {
        get
        {
            return String.Format("{0} {1}", Student.FirstName, Student.LastName);
        }
        set
        {
            if (StudentFullName.Length &gt; 1)
            {
                string[] tokens = StudentFullName.Split(' ');
                Student.FirstName = tokens[0];
                Student.LastName = tokens[tokens.Length - 1];
            }
        }
    }

    public String UserName { get; set; }

    [DataType(DataType.Password)]
    public String Password { get; set; }

    [DataType(DataType.Password)]
    [Compare("Password",ErrorMessage="Passwords do not match")]
    [Display(Name="Confirm Password")]
    public String C_Password { get; set; }
}
</code></pre>

<p>In the context database initializer class below is my seed method :</p>

<pre><code>  protected override void Seed(StudentContext context)
        {
            var departments = new List&lt;Department&gt;
            {
                new Department {DepartmentID=1,DepartmentName="CSE",DepartmentCode="CS101"},
                new Department {DepartmentID=2,DepartmentName="ECE",DepartmentCode="EC197"},
                new Department {DepartmentID=3,DepartmentName="MECH",DepartmentCode="MC202"}
            };

            departments.ForEach(d =&gt; context.Departments.Add(d));
            context.SaveChanges();

            var students = new List&lt;StudentViewModel&gt; {
                new StudentViewModel {StudentId=1,StudentFullName = "Zack Dyas",UserName ="zack_dyas",DepartmentID=1,Password ="zack123"},
                new StudentViewModel {StudentId=2,StudentFullName = "Abraham D'Pedro",UserName ="ab_dpedro",DepartmentID=2,Password ="ab123"}
            };

            students.ForEach(s =&gt; context.Students.Add(s));
            context.SaveChanges();
}
</code></pre>

<p>In the view how would I display the department name instead of the department id that is in the model for the 2 student records ?</p>