ASP.Net MVC Tute 4 - Create a List from IList

ASP.Net MVC Tute 4 - Create a List from IList

In this post I'll explain how to show data in a list.
I have define this inside the Controller's Item ActionResult. You can arrange these in proper locations.

1. Create IList using a Model.

        private static IList<Item> items = new List<Item>()
        {
            new Item()
            {
                Id = 1,
                ItemName = "iPhone",
                Price = 99000
            },
            new Item()
            {
                Id = 2,
                ItemName = "Lumia 520",
                Price = 25000
            },
            new Item()
            {
                Id = 3,
                ItemName = "xBox 360",
                Price = 50000
            }
        };

2. Pass that list to a View

        public ActionResult Item()
        {
            return View(items);
        }

3. Show list data within the view.

Use IEnumerable to iterate through the list
@model IEnumerable<ERP.Models.ItemModels.Item>

And get the item details:

@model IEnumerable<ERP.Models.ItemModels.Item>

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Item</title>
</head>
<body>
    <div>
        <h3>This is the Item View.</h3>
        <br/>
        <ul>
            @foreach (var item in Model)
            {
                <li>ID: @item.Id</li>
                <li>Name: @item.ItemName</li>
                <li>Price: @item.Price</li>
                <br/>
            }
        </ul>
    </div>
</body>
</html>

This will result the following.






Comments

Popular posts from this blog

Deploy Angular 8 app to Azure with Azure DevOps

Apache ActiveMQ 5 with .Net Core 3.1 and C#

Firebase with.Net Core and C#