Introduction:
Here I will explain how to retrieve data from database and show in a table in MVC 4.0 using Razor engine.
Description:
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.
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
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.
In the EmployeeController write down the code.
Now run your project and enter the URL localhost://Employee/Index
Download the full source code here.
Here I will explain how to retrieve data from database and show in a table in MVC 4.0 using Razor engine.
Description:
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 { [Table("tblEmployee")] public class Employee { [Key] 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 DbSetemployee { 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.name @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(); Listemployee1 = 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 Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
6 comments :
please Provide More MVC tutorials
please provide mvc more articles
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
my mailid was haribabuankasala@gmail.com make sure that i will get a reply from you thankyou
www.n-b-s.in
Thank u....
Note: Only a member of this blog may post a comment.