aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Show data from a database table content in MVC 4.0 razor

Dec 9, 2014

Here I will explain how to retrieve data from database and show in a table in MVC 4.0 using Razor engine.


So first create a new MVC project. Select Razor as your View engine.
Create a new database named what ever you want and add a new table named tblEmployee.Design your database as following.

And after that add an EmployeeController in the Controller folder.

And after that add an Empployee.cs in the Model folder. Add the following code into the Employee.cs.

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace MvcApplication1.Models
    public class Employee
        public int empid { get; set; }
        public string name { get; set; }
        public string dept { get; set; }


I set the Table attribute to tblEmployee as to connect the tblEmployee table with Employee class. Normally it takes the class name as the class name. But here our class name is differ from class name. So I did it.
Now add another class named EmployeeContext.cs into the Model folder again. Its is to connect the connection string in Web.Config file.
Write down the following code in the EmployeeContext.cs
using System.Data.Entity;

public class EmployeeContext : DbContext
    public DbSet employee { get; set; }

Add the connection string in the web.config as a name EmployeeContext. Now come back to EmpployeeController. Add a View of Indes Action. Doing this choose create a strongly typed view and select Employee. If Employee is not showing then do not get panic. Just build the solution once. It will show the Employee in to the drop down list to choose. Now in the Index.cshtml in View add the following code.

@using MvcApplication1.Models;
@using System.Web.Mvc;

@model IEnumerable

    ViewBag.Title = "Index";

All Employee Details

@foreach(Employee emp in @Model) { }
@emp.dept @emp.dept

In the EmployeeController write down the code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
    public class EmployeeController : Controller
        public ActionResult Index()
            EmployeeContext emp = new EmployeeContext();
            List employee1 = emp.employee.ToList();

            return View(employee1); // return list of employee

Now run your project and enter the URL localhost://Employee/Index

Download the full source code here.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


Unknown said...

please Provide More MVC tutorials


please provide mvc more articles

Unknown said...

hi sir i have a problem that i think surely you can help me. i want a small example that all crud operation should be performed without scaffolding and model binder and given example by you should be intereact with the database.and here you should use cod first approach. and i don't know how to declare class name and also i want know how to write connection string . thank you in advance

Unknown said...

my mailid was make sure that i will get a reply from you thankyou

Unknown said...

Arvind upadhyay said...

Thank u....

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.