TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Guest User
Tech Writer
111
10.1k
How to use common foreach loop for multiple models in MVC
Mar 26 2019 7:21 PM
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<Mobile> mobiles { get; set; }
public List<Computer> computers { get; set; }
}
}
Controller Class
public class HomeController : Controller
{
List<Mobile> mob = new List<Mobile>()
{
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<Computer> com = new List<Computer>()
{
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";
}
<h2>Index</h2>
<table>
<tr>
<th> Id</th>
<th>Name</th>
<th> Price</th>
</tr>
@foreach(var item in Model)
{
<tr>
<td>@item.Id</td>
<td>@item.mobileName</td>
<td>@item.mobilePrice</td>
</tr>
}
</table>
So I am confused how to inherit from Viewmodel class and display in single foreach for mutiple models
Reply
Answers (
4
)
how to display images in rdlc.Report
web Site issue when deploying on server side.