View model a is simple class which can contain more than one class property. We use it to inherit all the required properties, e.g. I have two classes Student and Subject
Public class Student
{
public int Id {get; set;}
public string Name {get; set;}
}
Public class Subject
{
public int SubjectID {get; set;}
public string SubjectName {get; set;}
}
Now we want to display records student's Name and Subject's Name in View (In MVC), but it's not possible to add more than one classes like:
@model ProjectName.Model.Student
@model ProjectName.Model.Subject
the code above will throw an error...
Now we create one class and can give it any name, but this format "XyzViewModel" will make it easier to understand. It is inheritance concept. Now we create a third class with the following name:
public class StudentViewModel:Subject
{
public int ID {get; set;}
public string Name {get; set;}
}
Now we use this ViewModel in View
@model ProjectName.Model.StudentViewModel
Now we are able to access all the properties of StudentViewModel and inherited class in View.