Below is my model class
namespace WebApplication3.Models
{
public class Mobile : ViewModelMobCom
{
public int Id { get; set; }
public string Name { get; set;}
public int Price { get; set; }
}
public class Computer : ViewModelMobCom
{
public int Id { get; set; }
public string Name { get; set; }
public int Price { get; set; }
}
public class ViewModelMobCom
{
public List mobiles { get; set; }
public List computers { get; set; }
}
}
Controller Class
public class HomeController : Controller
{
List mob = new List()
{
new Mobile() { Id=101,Name="Vivo",Price=15000 },
new Mobile() { Id=102,Name="Oppo",Price=16000 },
new Mobile() { Id=103,Name="1plus",Price=21000 },
new Mobile() { Id=104,Name="Nokia",Price=10000 },
new Mobile() { Id=105,Name="Samsung",Price=12000 },
new Mobile() { Id=106,Name="MI",Price=8000 }
};
List com = new List()
{
new Computer() { Id=101,Name="HP",Price=20000 },
new Computer() { Id=102,Name="Lenovo",Price=25000 },
new Computer() { Id=103,Name="Dell" ,Price=30000}
};
public ActionResult Index()
{
ViewModelMobCom vmmobcom = new ViewModelMobCom();
vmmobcom.computers = com;
vmmobcom.mobiles = mob;
return View(vmmobcom);
}
}
}
Index Page
model WebApplication3.Models.ViewModelMobCom
@{
ViewBag.Title = "Index";
}
Index
| Id | Name | Price |
|---|---|---|
| @item.Id | @item.mobileName | @item.mobilePrice |
So I am confused how to inherit from Viewmodel class and display in single foreach for mutiple models
brijesh rathorPosted Mar 27, 2019, 5:16 AM
Index
brijesh rathorPosted Apr 18, 2019, 2:25 AM
Guest UserPosted Apr 12, 2019, 12:39 AM
Jignesh KumarPosted Mar 26, 2019, 9:14 PM