In this article we will cover how to List Data in Asp.Net Core 3.1 Application.
Let's Start:
Models
Create below mentioned classes inside Models folder
public class City { public int City_Id { get; set; } public string City_Title { get; set; } public string City_Code { get; set; } public int State_Id { get; set; } public string State_Title { get; set; } public int Country_Id { get; set; } public string Country_Title { get; set; } }
Controller
Create Controller named "CityController" and add below mentioned methods.
public IActionResult Index() { IEnumerable<City> lstCity = new List<City>(); HttpResponseMessage res = client.GetAsync($"City").Result; if (res.IsSuccessStatusCode) { var result = res.Content.ReadAsStringAsync().Result; lstCity = JsonConvert.DeserializeObject<IEnumerable<City>>(result); } return View(lstCity); }
Views
Create "Index.cshtml" inside "City" Folder and add below mentioned html
@{ @model IEnumerable<City> } <table> <thead> <tr> <th>City Id</th> <th>Country Id</th> <th>State Id</th> <th>City Title</th> <th>City Code</th> <th>State Title</th> <th>Country Title</th> </tr> </thead> <tbody> @foreach (var item in Model) { <tr> <td>@item.City_Id</td> <td>@item.Country_Id</td> <td>@item.State_Id</td> <td>@item.City_Title</td> <td>@item.City_Code</td> <td>@item.State_Title</td> <td>@item.Country_Title</td> </tr> } </tbody> </table>
Note: In this article i have used API to get city data. You can do this using any method. (Call API or access data from database without API)
Post Comments(0)