Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Showing Data in MVC 4 using WCF service

Article:
Viewed:  1881 
Posted On:  11/01/2014 11:22:14 
How we can show data in MVC 4 applicaton uisng WCF and ADO.NET Entity framework ? 

In this article I am going to explain how we show data in a MVC application using WCF service.

Create a WCF Service Application.

MVC With WCF 1.png


Image 1.

Now right click on Solution Explorer -> Add New Item

MVC With WCF 2.png

Image 2.

MVC With WCF 3.png

Image 3.

Click New Connection -> Select Server Name -> Provide Authentication -> Select Data Base -> OK

MVC With WCF 4.png

Image 4.

MVC With WCF 5.png

Image 5.

MVC With WCF 6.png

Image 6.

MVC With WCF 7.png

Image 7.

Now Open Iservice1.cs file

Remove GetData() and GetDataUsingDataContract() method. Now Iservice1.cs should be like below, I added a new Interface GetEmployee().

MVC With WCF 8.png

Image 8.

Now Open Service1.svc.cs file

Remove GetData() and GetDataUsingDataContract() from here and implement GetEmployee() interface here.

MVC With WCF 9.png

Image 9.

Now Run the application.

MVC With WCF 10.png

Image 10.

 Now Create a MVC 4 application Name as MyMVCAppWithWCF

MVC With WCF 11.png

Image 11.

MVC With WCF 12.png

Image 12.

Now Right Click on Solution Explorer -> Add Service Reference.

MVC With WCF 13.png

Image 13.

MVC With WCF 14.png

Image 14.

Now Right Click on Controller -> Add Controller

MVC With WCF 15.png

Image 15.

Now  in Index do the following code:

MVC With WCF 16.png

Image 16.

MVC With WCF 17.png

Image 17.

Our VIEW is:

@model 
IEnumerable<MyMVCAppWithWCF.MyWCFService.Employee>
@{
    ViewBag.Title = "Showing Data In MVC 4 using WCF Service";
}
<p>
    @Html.ActionLink("Create New""Create")
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.City)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Country)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Dept)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Emp_ID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Mobile)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.State)
        </th>
        <th></th>
    </tr>
 
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.City)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Country)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Dept)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Emp_ID)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Mobile)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Name)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.State)
            </td>
            <td>
                @Html.ActionLink("Edit""Edit"new { id = item.ID }) |
            @Html.ActionLink("Details""Details"new { id = item.ID }) |
            @Html.ActionLink("Delete""Delete"new { id = item.ID })
            </td>
        </tr>
    }
 
</table>

Now Run the application. . .

MVC With WCF 18.png

Image 18.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us