Search

Thursday, February 13, 2014
0 comments

How to Create Data Model using Database in MVC Application

5:59 PMThursday, February 13, 2014
As I have discussed in earlier article, MVC model folder have all the classes that may be used for application logic in the application. By default an MVC application have some of the models like AccountModels, LogOnModels, RegisterModels etc.

We will create a new model class by using some simple steps.

  • Right click on the Model folder and add a new class named Student.cs
  • Insert some properties in that class like written below.

namespace MvcApplication1.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
public class StudentContext: DbContext
{
public DbSet<Student> Students { get; set; }
}
}

This class have all the four properties which are the columns in the student table of the database. Now to work with this class we have to add a controller (after debugging this application) class in the Controllers folder with these simple steps.

  • Right click on the Controller folder and add a new Controller. A window will appear with some of the options to be inputted.
  • Name the controller StudentController and more options like shown in the window.

How to Create Data Model using Database in MVC Application


After clicking on Add button visual web developer will add StudentController.cs file under the Controller folder, and Student folder under the Views folder.

The last steps of this article is to add Database views which are used for UI purpose in the application. There is a plus point in this MVC application i.e. we don’t need to add required views because they are automatically added by the visual web developer.

Lookout under the Views>Student Folder

  • Index.cshtml
  • Create.cshtml
  • Delete.cshtml
  • Details.cshtml
  • Edit.cshtml


Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top