0

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.

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

}

and

 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<Student> Students { get; set; }
    }

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

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

In the context database initializer class below is my seed method :

  protected override void Seed(StudentContext context)
        {
            var departments = new List<Department>
            {
                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 => context.Departments.Add(d));
            context.SaveChanges();

            var students = new List<StudentViewModel> {
                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 => context.Students.Add(s));
            context.SaveChanges();
}

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 ?

0

2 Answers 2

1

ViewModel means it's a DTO, data transfer object, it's not a DB entity but a class with the properties needed for the specific view you're targeting.
In most cases you should have 1 ViewModel for each view and shouldn't reuse the same model across several views.

So create StudentXModel with DepartmentName when you need the Name, and StudentYModel with DepartmentId when you need the Id.

See more here

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

2 Comments

But when I will create tables out of this viewmodel that contains departmentname will it not violate entity relationship ? Ideally foreign key should be departmentid . So even if I make departmentname as part of viewmodel while storing it should be departmentid back to the table ? Right ? How would I do that .
But when I will create tables out of this viewmodel that contains departmentname will it not violate entity relationship You should never create tables out of view models, this is not the way they should be used. You're using the ViewModel term without understanding what it really is all about. Please read the links I linked.
1

Use a ViewModel

public class StudentViewModel
{
    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; }

    public int DepartmentID { get; set; }
    public String DepartmentName { get; set; }
}

Fill this ViewModel in the controller, instead returning a Student return a StudentViewModel. Remember to fill the DepartmentName property in your logic (at this point you should be able to get the DepartmentName value using the DepartmentID).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.