BLOGs
Technology
Published August 08th, 2016 by

Parsing Restful Service in MVC Views with Pagination

Share on FacebookShare on LinkedInTweet about this on TwitterShare on Google+Pin on Pinterest

This Article will explain how we can consume the Restful service in simpler way with less coding in Asp.Net MVC.
Restful service is the light weighted and high scalable service and also it is not dependent on Platform and not tied with any Particular technologist. Common data formats are XML, JSON and RSS.

Normally all the business logics will be written on the Controller class In Asp.net MVC. So in this Article I have written the code for consuming Service in Controller Action Result method.

Here is the sample Restful service Json output. Creation of Rest service is not part of this article so I am taking the existing service which I have created.

JSON output

Here is the Country Model Class, those properties I am going to consume from the above Country web service.

public class Country

{

            public string country {get; set;}

            public string Name {get; set;}

            public string Abbr { get; set; }

            public string capital {get; set ;}

}

Once we read the Restful service it will return Web response output in the form of string, for read Parsing those value node by node, we need to convert into Array. For that we need Json converter. Here I am using Json.Net library, which is used to convert Json data into Array by using “DeserializeObject” method. Json.Net Library is available in Visual studio NuGet Packages.

JSON net1

Here is the ActionResult Method. This method will consume the above web service and Parse the data node by node using above Model class “Country”. And it will be stored in the Generic List. Consumed data is shared to the view as ViewBag. ViewBag is the dynamic object, it doesn’t have any predefined property, so user can set our own property, use this property in View to display the result.

public ActionResult Index (int? Page)
{
int PageNumber = Page?? 1;
string Serviceurl = “http:// http://localhost:55946/state/get/IND/all”;
string _strjsondata = GetData(Serviceurl);
dynamic array = JsonConvert.DeserializeObject(_strjsondata);
dynamic ContList = array.RestResponse.result;
List<Country> li = new List<Country>();
foreach (var con in ContList)
{
Country c = new Country();
c.country=con[“country”];
c.Name = con[“name”];
if (con[“abbr”]!= null)
{
c.Abbr = con[“abbr”];
}

c.capital = con[“capital”];
li.Add(c);
}
//ViewData[“Country”] = li;
ViewBag.Country = li.ToPagedList(PageNumber, 10);
return View();
}
public string GetData(string Serviceurl)
{
Uri Serviceuri = new Uri(Serviceurl);
HttpWebRequest Webrequest = (HttpWebRequest)HttpWebRequest.Create(Serviceuri);
Webrequest.Method = WebRequestMethods.Http.Get;
HttpWebResponse Webresponse = (HttpWebResponse)Webrequest.GetResponse();
StreamReader Strreader = new StreamReader(Webresponse.GetResponseStream());
string Stroutput = Strreader.ReadToEnd();
Webresponse.Close();
return Stroutput;
}
Here I am storing consumed data as form of list in ViewBag country property. Above Restful server will return more no of data, so we need to incorporate the Paging option for the Grid in View. Here the code snippet for Paging.
ToPageList method is used for implement the Paging option in View and maximum no record in each is set to 10.
ViewBag.Country = li.ToPagedList(PageNumber, 10);

Here is the View code. Here I am reading ViewBag Country property in for loop and dynamically bind the data to the table.
Paging will be added at the bottom of the page.
@Html.PagedListPager((IPagedList)ViewBag.Country, page => Url.Action(“Index”, new { page }))

@{
ViewBag.Title = “Index”;
}
@using PagedList;
@using PagedList.Mvc;
<h2>Index</h2>
@{
//var con = (List<MvcApplication3.Controllers.Country>) ViewData[“Country”]; // Cast the list
}
<style>

table, th, td {
border: 1px solid black;

}

th,td {
height: 20px;
vertical-align: bottom;
line-height:20px;
text-align:center;
}
</style>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<table style=”border: 1px solid black;” >

<tr><th> Country </th><th>Name</th><th>Abbr</th><th>Capital</th></tr>
@foreach (var item in ViewBag.Country)
{

<tr><td>@item.country</td>
<td>@item.Name</td>
<td>@item.Abbr</td>
<td>@item.capital</td></tr>

}
</table>
//@Html.PagedListPager(ViewBag.Country, page => Url.Action(“Index”, new { page }))
@Html.PagedListPager((IPagedList)ViewBag.Country, page => Url.Action(“Index”, new { page}))

}

OP3

Conclusion
Restful service is used in most of the Internet application and API. Because it is light weighted and independent of Technology.

This article explained how we can consume the Restful ASP.NET development service and displayed in the View with Pagination in Asp.net MVC.

The below functionalities are covert in this article.
Reading Restful service and convert to Generic list format.
Passing Generic list from Controller to view using Viewbag.
Parse the Viewbag data and displayed in Grid format
Implemented Pagination for the displayed grid values.

Technoligent

Technoligent

Director at Technoligent
Chirag Thumar is work with Technoligent as a java application and software developer for many years. He major contribute included the collection of web solutions like java, Python, Asp.Net, Mobile Apps etc…. He is extensive experience as java web development and object oriented programming language. You can follow me on twitter @Techno_Ligent and like our Facebook page TechnoLigent
Technoligent

Our rankings are completely independent, transparent, and community driven; they are based on user reviews and client sentiment. These technology companies had to earn their way up and didn't just pay their way up.

View Rankings of Best Technology Companies