Search

Thursday, April 9, 2015
0 comments

Access data from database table in MVC

6:54 PMThursday, April 9, 2015
In MVC, we can access data from database easily. When we send any request to the server, respond generated by the controller. Data access from the Model and render by the View. First to make a database table using visual studio 2010 or higher. we have database table that is "Employee", in which have some attributes, these are

CREATE TABLE [dbo].[Employee]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY,
[Name] NVARCHAR(50) NULL,
[Age] INT NULL
)


  1. First to install entity framework by the "Nuget packages", which is available in tools-->Library Package Manager
  2. This is already available in 4.5 framework or 4.5.1.
ntity framework by the "Nuget packages
  1. Now, add the employee class in model folder with some public properties  like
  2.  
     namespace WebApplication10.Models
    {
    public class Employee
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public int age { get; set; }
    }
    }
    
    
  3. Now, add another class which is EmployeeContext.cs class, which is inherit from DBContext class. DBContext class is available in System.Data.Entity namespace. This class is used for making connection. Now, your code look like
  4.  
    using System.Data.Entity;namespace WebApplication10.Models
    {
    public class EmployeeContext: DbContext
    {
    public DbSet<Employee> empl { get; set; }}
    }
    
    Here, empl is the public property, by which we can create the connection to the database.
  5. Now, add the ConnectionString in the web.config file that is
    <add name="EmployeeConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\Database1.mdf;Initial Catalog=Database1;Integrated Security=True"
    providerName="System.Data.SqlClient" />
    
    
  6. Map the database table with the Employee class using Table class which is available in  System.ComponentModel.DataAnnotations.Schema; namespace, after mapping now your code look like
    using System.ComponentModel.DataAnnotations.Schema;namespace WebApplication10.Models
    {[Table("Employee")]
    public class Employee
    {
    public int ID { get; set; }
    public string Name { get; set; }
    public int age { get; set; }
    }
    }
    
  7. Now, create a EmployeeController  in controller folder, Which is inherit from Controller class. This Controller class is exists in System.Web.Mvc namespace. Also create a public method, which return ActionResult class. Now your code look like
     using System.Linq;
    using System.Web.Mvc;
    using WebApplication10.Models;
    
    namespace WebApplication10.Controllers
    {
    public class EmployeeController : Controller
    {
    //
    // GET: /Employee/
    public ActionResult Index()
    {
    EmployeeContext context = new EmployeeContext();
    Employee emp = context.pl.Single();
    
    return View(emp);
    }
    }
    }
    
    Here we create a instance of EmployeeContext class and invoke the public property of that class.
  8. Now create the view for this program. Before adding the view with their presentation must build the project. Learn How to create the new view
 create the view for this program

  1. Prepare the view and access all the properties of the employee class by the @Model properties.
     @model WebApplication10.Models.Employee
    
    @{
    ViewBag.Title = "Employee Details";
    }
    
    <h2>Employee Details are:</h2>
    Employee Id : <p>@Model.ID</p>
    Employee Name : <p>@Model.Name</p>
    Employee Age : <p>@Model.age</p>
    
    
  2. Now run the code and check your desired output
Access data from database table in MVC

Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top