Trying to make a simple application but my view returns nothing when trying to use a viewmodel. I assume the "db.[TableName].ToList();", which works when applied on a domain model, is not enough and the selection should happen in a different way when using a viewmodel, but I have no idea how to do it. Please help. Thank you.
Town.cs
using System.Collections.Generic;
namespace City.Models
{
public class Town
{
public Town()
{
Streets = new List<Street>();
}
public int TownId { get; set; }
public string TownName { get; set; }
public virtual ICollection<Street> Streets { get; set; }
}
}
Street.cs
using System.Collections.Generic;
namespace City.Models
{
public class Street
{
public Street()
{
Houses = new List<House>();
}
public int StreetId { get; set; }
public string StreetName { get; set; }
public virtual ICollection<House> Houses { get; set; }
}
}
House.cs
namespace City.Models
{
public class House
{
public int HouseId { get; set; }
public string HoueseName { get; set; }
public int StreetId { get; set; }
public virtual Street Street { get; set; }
}
}
Floor.cs
namespace City.Models
{
public class Floor
{
public int FloorId { get; set; }
public int FloorNumber { get; set; }
public int FireExtinguisherId { get; set; }
}
}
FireExtinguisher.cs
namespace City.Models
{
public class FireExtinguisher
{
public int FireExtinguisherId { get; set; }
public string FireExtinguisherName { get; set; }
public int FloorId { get; set; }
}
}
MyViewModel.cs
namespace City.Models
{
public class MyViewModel
{
public MyViewModel()
{
Town = new Town();
Street = new Street();
House = new House();
Floor = new Floor();
FireExtinguisher = new FireExtinguisher();
}
public int MyViewModelId { get; set; }
public Town Town { get; set; }
public Street Street { get; set; }
public House House { get; set; }
public Floor Floor { get; set; }
public FireExtinguisher FireExtinguisher { get; set; }
}
}
ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<Town> Towns { get; set; }
public DbSet<Street> Streets { get; set; }
public DbSet<House> Houses { get; set; }
public DbSet<Floor> Floors { get; set; }
public DbSet<FireExtinguisher> FireExtinguishers { get; set; }
public DbSet<MyViewModel> MyViewModels { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
HomeController.cs (I think the problem lies here)
using System.Linq;
using System.Web.Mvc;
using City.Models;
namespace City.Controllers
{
public class HomeController : Controller
{
private ApplicationDbContext db;
public HomeController()
{
db = new ApplicationDbContext();
}
public ActionResult Index()
{
return View(db.MyViewModels.ToList());
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
}
Index.cshtml
@model IEnumerable<City.Models.MyViewModel>
<h2>Map information</h2>
<div class="container">
<table class="table">
<thead>
<tr>
<th>Town</th>
<th>Street</th>
<th>House</th>
<th>Floor</th>
<th>FireExtinguisher</th>
</tr>
</thead>
@foreach (var item in Model)
{
<tbody>
<tr>
<td>@(item.Town.TownName)</td>
<td>@(item.Street.StreetName)</td>
<td>@(item.House.HoueseName)</td>
<td>@(item.Floor.FloorNumber)</td>
<td>@(item.FireExtinguisher.FireExtinguisherName)</td>
</tr>
</tbody>
}
</table>
</div>
Even though I have test data in the db, this is all what I see when I run it:
Image is here
Please tell me what should I fix, how to get data retrieved. Thanks
EDIT @CrowdCoder
new picture here