As I said in the previous article Getting Started with ASP.NET MVC 3, Models implements the logic of data domain of an application. It defines the way how the data should interact with the users of the system. In another way, ASP.NET models assure that the data are in the appropriate format before it gets inserted or retrieved from a database. It does not mean that the Databases are the only interacting component of the model. Users can simply create data objects as well. The beauty of MVC 3 is that it facilitates the Model to be modeled with Entity Framework. So it can be done within a few clicks.
To create a Model in the Models folder, right-click on it and follow “Add–>Class”. Then give a name for the .cs or .vb file. For the moment, I will create a class named Student.
To start it simply, a basic Model class can be defined like this.
public class Student
{
public int studentID { get; set; }
public string FirstName { get; set; }
public string Lastname { get; set; }
public DateTime DateOfBirth { get; set; }
public string ResidentialAddress { get; set; }
}
Here I have grabbed the a few essentials that a student must have. For the moment, lets think these are enough to model the student. Then, we are done with the model. But, remember there are many validations needed to be done. Let’s discuss them later.
GET:/Student/
It does mean that to invoke the Index method of the Student Controller, you must type the “Root level URL/Student/”. This is as per the default setting. But this controller will not work till we add a View. Creating a View for this project has two steps. First thing is creating a sub folder named “Student” inside the “Views” folder. It is the folder related to the Student Controller. To invoke the Index method, you must create a View named Index.
There are many templates that I can select depending on my requirement. To create a form to enter the student data, I do select it as Create. Then click Add button. The other templates are for viewing student in a list and etc… I do have the option of selecting the master page as well.
Once you click the Add button, you will see the View is being created. Now I do have a form to enter the student details. This is not yet connected to the database. Lets discuss the way how it happens in next article.
Just click the Debug button (F5) and type the “Student/” and see what you have got.
Eg: http://localhost:57955/Student/ (Your port number may be different.)