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 rathor165 reputationPosted Mar 27, 2019, 1:16 AM EDT
Index
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 26, 2019, 5:14 PM EDT
Guest User111 reputationPosted Apr 11, 2019, 8:39 PM EDT
brijesh rathor165 reputationPosted Apr 17, 2019, 10:25 PM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts