Monday, January 8, 2018

Sample stuff

Model class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication2.Models
{
    public class ModelTest
    {
        public int ID { get; set; }

        public string ItemName { get; set; }

        public double Price { get; set; }

        

    }
}

Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class TestController : Controller
    {
        // GET: Test
        public ActionResult Index()
        {
            ModelTest mt = new ModelTest();
            mt.ID = 1;
            mt.ItemName = "item1";
            mt.Price = 25.00;
            ModelTest mt1 = new ModelTest();
            mt1.ID = 2;
            mt1.ItemName = "item2";
            mt1.Price = 5.00;
            ModelTest mt2 = new ModelTest();
            mt2.ID = 3;
            mt2.ItemName = "item3";
            mt2.Price = 13.00;
            List<ModelTest> items = new List<ModelTest>();
            items.Add(mt);
            items.Add(mt1);
            items.Add(mt2);

            return View(items);
        }
    }
}

the web page

@model  IEnumerable<WebApplication2.Models.ModelTest>
@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>
<table class="table">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Price</th>
    </tr>
    @foreach (var item in Model)
    {
    <tr>
        <td>@Html.DisplayFor(model => item.ID)</td>
        <td>@Html.DisplayFor(model => item.ItemName)</td>
        <td>@Html.DisplayFor(model => item.Price)</td>
    </tr>
    }
</table>

No comments:

Post a Comment