|

Push and Pull data from Restful Service in Asp.net MVC Application using Angular JS

In this post, asp.net development India based professionals will explain the best way to manage the Restful service using AngularJS. They are explaining the push and pull functionality available in Rest service in simple steps. Read the article to know more.

In this Post I am going explain how we can manage the Restful service using AngularJS. Push and Pull functionality of Rest service Json data in MVC View in easy steps.

Most of the Pull and Push Services will be happened in Serve side. Each server round trips the user has to wait until unless the data will be loaded in the UI. To avoid this we can use the client side scripting like AngularJS.
AngularJS is the JavaScript framework. It is a structural framework which make use of Html as a template language and make you application is dynamic.
We are going touch the below points in this Article.
1. Adding AngularJS scripts from NuGet Packages
2. Creating JSon Service through Entity Framework
3. Reading JSon Rest Service and Displayed in MVC View
4. Implementing Paging in AngularJS

Adding AngularJS Script files
First we need to add the AngularJS reference scripts from Visual Studio through Mange NuGet Packages.

Create Table and fill data for Rest service.
Create PartMaster table using below SQL script.

CREATE TABLE [dbo].[PartMaster] (
[Id] INT NOT NULL,
[PartNum] NVARCHAR (50) NULL,
[Name] NVARCHAR (MAX) NULL,
[Supplier] NVARCHAR (50) NULL,
[Type] NCHAR (10) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);

Fill the bellow data in PartMaster table.

Create the Entity data model for the above Table.

Create MVC controller PartJsonController class under App_Start folder. Copy and paste the below code in Controller class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace MvcApplication3.App_Start
{
public class PartJsonController : Controller
{
//
// GET: /PartJson/
public ActionResult Index()
{
return View();
}
public JsonResult GetParts()
{
Entities2 PartEntity = new Entities2();
var result = PartEntity.PartMasters.ToList();
return Json(result, JsonRequestBehavior.AllowGet);

}
}
}

GetParts method will read the data from Entity and return JSon Data. Right click on the method and create a New MVC View page using Add View context menu.
The output of Rest service as shown below.

Create JavaScript file “ShowParts.JS” under Script folder.
Copy the below code and paste in ShowPart JS file.

var PartModule = angular.module('PartMaintenace', ['ui.bootstrap', 'ngResource']);
PartModule.controller('PartController', function ($scope, getpartSvc) {
$scope.reverse = true;
$scope.predicate = 'name';
$scope.currentPage = 1;
$scope.totalItems = 0;
$scope.numPerPage = 0;
$scope.order = function (predicate) {
$scope.reverse = ($scope.predicate === predicate) ? !$scope.reverse : false;
$scope.predicate = predicate;
};
GetPartInfos();
function GetPartInfos() {

getpartSvc.GetPartInfos()
.success(function (parts) {

$scope.GetParts = parts;

$scope.totalItems = $scope.GetParts.length;
$scope.numPerPage =5;
$scope.paginate = function (value) {
var begin, end, index;
begin = ($scope.currentPage - 1) * $scope.numPerPage;
end = begin + $scope.numPerPage;
index = $scope.GetParts.indexOf(value);
return (begin <= index && index < end);
};
console.log($scope.parts);
})
.error(function (error) {
$scope.status = error.message;
console.log($scope.status);
});

}

});

PartModule.factory('getpartSvc', ['$http', function ($http) {

var GetpartSvc = {};
GetpartSvc.GetPartInfos = function () {
return $http.get('/PartJson/getparts');
};

return GetpartSvc;

}]);

The bellow portion is used for read the JSon data from Rest service “getParts”.

PartModule.factory('getpartSvc', ['$http', function ($http) {

var GetpartSvc = {};
GetpartSvc.GetPartInfos = function () {
return $http.get('/PartJson/getparts');
};

return GetpartSvc;

}]);

Next we have to create the new MVC View to display the Restful service data.
Refer the above Script file for fetching the Rest service data and also refer the AngularJS and Bootstrap css file in Script section of MVC View.

@section scripts{
<script src=”~/Scripts/angular.js”></script>
<link rel=”stylesheet” href=”~/Scripts/bootstrap.min.css”>

<script src=”~/Scripts/angular-ui/ui-bootstrap-tpls.js”></script>

<script src=”~/Scripts/angular-resource.js”></script>
<script src=”~/Scripts/ShowParts.js”></script>

}

Copy the below code and Paste on your Index View Page under Views/PartJson

@{
ViewBag.Title = “Index”;
}

<h2>Index</h2>

@section scripts{
<script src=”~/Scripts/angular.js”></script>
<link rel=”stylesheet” href=”~/Scripts/bootstrap.min.css”>

<script src=”~/Scripts/angular-ui/ui-bootstrap-tpls.js”></script>

<script src=”~/Scripts/angular-resource.js”></script>
<script src=”~/Scripts/ShowParts.js”></script>

}
<div>

<style>
.odd {
background-color:palegreen;
color: #008b8b;
}
td th {
height: 30px;
min-width: 100px;
}
thead {
background-color:aqua;
color: white;
height: 30px;
}
</style>
</div>
<div ng-app=”PartMaintenace” >
<br/>
<br/>
@*<input type=”text” placeholder=”Part Search” ng-model=”PartSearch” />*@
<br />
<div ng-controller=”PartController” class=”container-fluid”>

<table class=”table table-striped” >
<thead>
<tr>
<th></th>
<th>
<a href=”” ng-click=”order(‘Id’)”>ID</a>
</th>
<th><a href=”” ng-click=”order(‘PartNum’)”> PartNum</a> </th>
<th><a href=”” ng-click=”order(‘Name’)”>Name</a> </th>
<th><a href=”” ng-click=”order(‘Supplier’)”>Supplier</a> </th>
<th><a href=”” ng-click=”order(‘Type’)”>Type</a> </th>
</tr>
</thead>
<tbody>
<tr>
<td>Search Filter =>></td>
<td > <input style=”width:100px;height:25px” type=”text” ng-model=”PartSearch.Id” /></td>
<td> <input style=”width:100px;height:25px” type=”text” ng-model=”PartSearch.PartNum” /> </td>
<td><input style=”width:100px;height:25px” type=”text” ng-model=”PartSearch.Name” /> </td>
<td><input style=”width:100px;height:25px” type=”text” ng-model=”PartSearch.Supplier” /> </td>
<td><input style=”width:100px;height:25px” type=”text” ng-model=”PartSearch.Type” /> </td>
</tr>
<tr ng-repeat=”p in GetParts| orderBy:predicate:reverse | filter:paginate | filter : PartSearch” ng-class-odd=”‘odd'”>
<td></td>
<td>{{p.Id}}</td>
<td>{{p.PartNum}}</td>
<td>{{p.Name}}</td>
<td>{{p.Supplier}}</td>
<td>{{p.Type}}</td>
</tr>
</tbody>
</table>
<pagination total-items=”totalItems” ng-model=”currentPage”
max-size=”1″ boundary-links=”true”
items-per-page=”numPerPage” class=”pagination-sm”>
</pagination>

</div>

</div>

Output screen as shown bellow

Conclusion

This Article is explained about the Rest service JSon data manipulation through AngularJS. AngularJS is the latest scripting framework, which is used to interact with server side data and displayed in UI without any Post back.

This Article contains 4 steps as below

  1. Read the data from database through Entity Framework
  2. Returning Json Data from MVC Controller
  3. AngularJS Service for fetch the JSon data
  4. AngularJS View for display the results
    This post will help you to get basic idea about AngularJS in Asp.net MVC application. Thanks for reading this article.
    We hope you have read all the points shared by asp.net development India based experts. If there is anything that is not clear or confusing you, then let us know in the comments.

Similar Posts

  • How to encode spaces in curl GET request URL on Linux?

    The problem is like this: I would like to send a GET request using curl and the content has spaces, such as curl “http://example.com/send?msg=hello world” The space and the “world” will be left away if this command is executed directory on Linux. How to encode spaces in curl request URL? You can use the –date-urlencode…

  • How to get hostname in Python on Linux?

    In Python, how to get hostname as the command hostname does on Linux? In Python, you can get the hostname by the socket.gethostname() library function in the socket module: import socket hostname = socket.gethostname() Reference: https://www.systutorials.com/dtivl/20/how-to-get-the-hostname-of-the-node?show=34#a34 Read more: How to get the hostname of the node in Python? Getting Hostname in Bash in Linux in…

  • 禁止对话框关闭按钮和Alt+F4

    在某些情况下我们需要防止用户单击窗口的标题栏中的关闭按钮关闭 MFC 应用程序。可以删除窗口的WS_SYSMENU 样式, 但是,这样最大化最小化和还原按钮也被删除,并且无法添加。 这是Windows的设计依据。 可以通过禁用关闭按钮来模拟没有关闭按钮的窗口。 在 WM_CREATE 消息处理程序中禁用关闭按钮。使用下面的代码: CMenu *pSysMenu = GetSystemMenu(FALSE); ASSERT(pSysMenu != NULL); VERIFY(pSysMenu->RemoveMenu(SC_CLOSE, MF_BYCOMMAND)); 这样删除之后关闭按钮变为灰色,用户无法点击。但是使用Alt+F4仍然可以关闭程序。要将此功能也禁用需要重载CDialog的OnSysCommand方法。代码如下: void MyDlg::OnSysCommand( UINT nID, LPARAM lParam ) { if ( ( nID & 0xFFF0 ) == IDM_ABOUTBOX ) { CAboutDlg dlgAbout; //if you have an about dialog dlgAbout.DoModal(); } //add the following code else if…

  • How to enable iptables on CentOS 7 / Fedora 20?

    iptables is plain old good. How to enable it after I disable firewalld? First, install the iptables-services package as root: # yum install iptables-services Then, start iptables service as root: # touch /etc/sysconfig/iptables # touch /etc/sysconfig/ip6tables # systemctl start iptables # systemctl start ip6tables # systemctl enable iptables # systemctl enable ip6tables Read more: How…

  • Run Firefox on Remote Host over SSH

    How to  run Firefox on Remote Host over SSH ? After $ ssh -X remotehost Run this script on remote host: #!/bin/bash export $(dbus-launch) export NSS_USE_SHARED_DB=ENABLED firefox -no-remote & This script credits to Waxborg: http://waxborg.servepics.com/opensuse/remote-administration/running-firefox-remotely-over-ssh. Read more: How to run screen on a Linux host reporting “Cannot make directory ‘/var/run/screen’: Permission denied”? Fixing “Remote Host…

Leave a Reply

Your email address will not be published. Required fields are marked *