filmov
tv
How to bind a list collection of objects Model to ASP NET MVC View (Part -11 )

Показать описание
How to bind a list (collection) of objects (Model) to ASP.NET MVC View
Requirement: Visual studio 2015 | MVC 5 version | SQL server Any version
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
Models:
namespace MVC_Sample.Models
{
public class Products
{
public int Product_id { get; set; }
public string Product_name { get; set; }
public int Unit_Price { get; set; }
}
}
Action method:
public ActionResult Product_List()
{
List Angular bracket (less than) Product Angular bracket (greater than) list_info = new List Angular bracket (less than) Product Angular bracket (greater than) ()
{
new Product() { Product_id=101, Product_name="Pepe Jeans Men Blue Vapour Slim Fit Low-Rise ", Unit_Price=2000 },
new Product() {Product_id=102, Product_name="SPYKAR Men Blue Skinny Fit Low-Rise", Unit_Price=2499 },
new Product() {Product_id=103, Product_name ="SOJANYA Men Grey & Blue Self Design Kurta ",Unit_Price=5499 },
new Product() {Product_id=104,Product_name="Shaftesbury London Men White Smart Slim Fit ",Unit_Price=1999 }
};
return View(list_info);
}
View:
@model IEnumerable Angular bracket (less than) MVC_Sample.Models.Product Angular bracket (greater than)
Angular bracket (less than) table class="table" Angular bracket (greater than)
Angular bracket (less than) tr Angular bracket (greater than)
Angular bracket (less than) th Angular bracket (greater than)
@Html.DisplayNameFor(model = Angular bracket (greater than) model.Product_id)
Angular bracket (less than) /th Angular bracket (greater than)
Angular bracket (less than) th Angular bracket (greater than)
@Html.DisplayNameFor(model = Angular bracket (greater than) model.Product_name)
Angular bracket (less than) /th Angular bracket (greater than)
Angular bracket (less than) th Angular bracket (greater than)
@Html.DisplayNameFor(model = Angular bracket (greater than) model.Unit_Price)
Angular bracket (less than) /th Angular bracket (greater than)
Angular bracket (less than) th Angular bracket (greater than) Angular bracket (less than) /th Angular bracket (greater than)
Angular bracket (less than) /tr Angular bracket (greater than)
@foreach (var item in Model) {
Angular bracket (less than) tr Angular bracket (greater than)
Angular bracket (less than) td Angular bracket (greater than)
@Html.DisplayFor(modelItem = Angular bracket (greater than) item.Product_id)
Angular bracket (less than) /td Angular bracket (greater than)
Angular bracket (less than) td Angular bracket (greater than)
@Html.DisplayFor(modelItem = Angular bracket (greater than) item.Product_name)
Angular bracket (less than) /td Angular bracket (greater than)
Angular bracket (less than) td Angular bracket (greater than)
@Html.DisplayFor(modelItem = Angular bracket (greater than) item.Unit_Price)
Angular bracket (less than) /td Angular bracket (greater than)
Angular bracket (less than) /tr Angular bracket (greater than)
}
Angular bracket (less than) /table Angular bracket (greater than)
Requirement: Visual studio 2015 | MVC 5 version | SQL server Any version
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
Models:
namespace MVC_Sample.Models
{
public class Products
{
public int Product_id { get; set; }
public string Product_name { get; set; }
public int Unit_Price { get; set; }
}
}
Action method:
public ActionResult Product_List()
{
List Angular bracket (less than) Product Angular bracket (greater than) list_info = new List Angular bracket (less than) Product Angular bracket (greater than) ()
{
new Product() { Product_id=101, Product_name="Pepe Jeans Men Blue Vapour Slim Fit Low-Rise ", Unit_Price=2000 },
new Product() {Product_id=102, Product_name="SPYKAR Men Blue Skinny Fit Low-Rise", Unit_Price=2499 },
new Product() {Product_id=103, Product_name ="SOJANYA Men Grey & Blue Self Design Kurta ",Unit_Price=5499 },
new Product() {Product_id=104,Product_name="Shaftesbury London Men White Smart Slim Fit ",Unit_Price=1999 }
};
return View(list_info);
}
View:
@model IEnumerable Angular bracket (less than) MVC_Sample.Models.Product Angular bracket (greater than)
Angular bracket (less than) table class="table" Angular bracket (greater than)
Angular bracket (less than) tr Angular bracket (greater than)
Angular bracket (less than) th Angular bracket (greater than)
@Html.DisplayNameFor(model = Angular bracket (greater than) model.Product_id)
Angular bracket (less than) /th Angular bracket (greater than)
Angular bracket (less than) th Angular bracket (greater than)
@Html.DisplayNameFor(model = Angular bracket (greater than) model.Product_name)
Angular bracket (less than) /th Angular bracket (greater than)
Angular bracket (less than) th Angular bracket (greater than)
@Html.DisplayNameFor(model = Angular bracket (greater than) model.Unit_Price)
Angular bracket (less than) /th Angular bracket (greater than)
Angular bracket (less than) th Angular bracket (greater than) Angular bracket (less than) /th Angular bracket (greater than)
Angular bracket (less than) /tr Angular bracket (greater than)
@foreach (var item in Model) {
Angular bracket (less than) tr Angular bracket (greater than)
Angular bracket (less than) td Angular bracket (greater than)
@Html.DisplayFor(modelItem = Angular bracket (greater than) item.Product_id)
Angular bracket (less than) /td Angular bracket (greater than)
Angular bracket (less than) td Angular bracket (greater than)
@Html.DisplayFor(modelItem = Angular bracket (greater than) item.Product_name)
Angular bracket (less than) /td Angular bracket (greater than)
Angular bracket (less than) td Angular bracket (greater than)
@Html.DisplayFor(modelItem = Angular bracket (greater than) item.Unit_Price)
Angular bracket (less than) /td Angular bracket (greater than)
Angular bracket (less than) /tr Angular bracket (greater than)
}
Angular bracket (less than) /table Angular bracket (greater than)