Monday, 7 October 2013

Fetch Data from Linq to sql Using ASP.NET MVC4 RAZOR

In this article am trying to create a ASP.NET MVC4 application using vs2012 which is display data from the LINQ and RAZOR

->First we have to create a table:

CREATE TABLE [dbo].[employee]
(
  id int Primary Key,
  name varchar(50),
  designation varchar(50),

)
->After create the table we need to insert the values 

INSERT INTO [dbo].[employeeVALUES ('1','raj','.net developer')
INSERT INTO [dbo].[employeeVALUES ('2','sa','sd')






-> After completion insertion we are going create simple MVC4 application using VS2012 and LINQ to SQL.

->Open the VS2012 create a New Project from the file option.

-> Select ASP.NET MVC4 Web Application and give name as "fetchdata" click on "ok " button








->Select Intranet application And Select Razor from the drop down click on "OK" Your application will be created.



->Go to the solution explorer choosae Model folder and right click on model select a new item .


 -> the window will appear you can choose option "Data" from the left side of panel and select the "Linq to sql" give name as emp.dbml. click on "ADD "  button.




->the emp.dbml will be displayed.

-> Go to the Server Explorer Add a New Connection Select your database.



-> Refresh Server Explorer  and Open Your Database Drag and Drop your table on  emp.dbml.


->Open the Home controller.cs file from Controller folder.
Write a Controller Code:  


->From the above code am creating the class "empDataContext" and object specified as em;

->Go to the views from the solution explorer and expand the views select the index.cshtml page from the Home folder and do some modification .


->Above code we will add the empdatacontext which is under the Models Class, and our table (employee) is under empdatacontext 


  
-> Execute the Project the Result will be displayed as




No comments:

Post a Comment