List Data in Asp.Net Core 3.1 Application

How to List or Display Data in Asp.Net Core 3.1 Application

November 5, 2020

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 { getset; }
    public string City_Title { getset; }
    public string City_Code { getset; }
    public int State_Id { getset; }
    public string State_Title { getset; }
    public int Country_Id { getset; }
    public string Country_Title { getset; }
}

Controller

Create Controller named "CityController" and add below mentioned methods.

public IActionResult Index()
{
    IEnumerable<CitylstCity = 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)

Leave a reply

Will not be displayed in comment box .

Loading...