Example MVC sample.
Model
public class tbl_Product
{
[Display(Name = "Name")]
public string ProductName { get; set; }
[Display(Name = "Rate")]
public decimal PurchaseRate { get; set; }
[Display(Name = "Qty")]
public int Qauntity { get; set; }
[Display(Name = "Value")]
public decimal Value
{
get
{
return PurchaseRate * Qauntity;
}
}
}
Controller
public class ProductController : Controller
{
public IActionResult Index()
{
List<tbl_Product> Items = new List<tbl_Product>()
{
new tbl_Product()
{
ProductName = "Product 1",
PurchaseRate= 25,
Qauntity= 4,
},
new tbl_Product()
{
ProductName = "Product 2",
PurchaseRate= 4,
Qauntity= 2,
},
new tbl_Product()
{
ProductName = "Product 3",
PurchaseRate= 5,
Qauntity= 3,
}
};
return View(Items);
}
}
View
@model IEnumerable<MvcDemo.Models.tbl_Product>
@{
ViewData["Title"] = "Index";
}
<h1>Index</h1>
<p>
<a asp-action="Create">Create New</a>
</p>
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.ProductName)
</th>
<th>
@Html.DisplayNameFor(model => model.PurchaseRate)
</th>
<th>
@Html.DisplayNameFor(model => model.Qauntity)
</th>
<th>
@Html.DisplayNameFor(model => model.Value)
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ProductName)
</td>
<td>
@Html.DisplayFor(modelItem => item.PurchaseRate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Qauntity)
</td>
<td>
@Html.DisplayFor(modelItem => item.Value)
</td>
</tr>
}
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td>Total Value :</td>
<td>@Model.Sum(p => p.Value)</td>
</tr>
</tfoot>
</table>