I want to reach multiple models in one view. I have DAL folder and DbContext.
class CvContext : DbContext
{
public CvContext() : base("CvContext")
{
}
public DbSet<LinkModel> Links { get; set; }
public DbSet<AboutModel> Abouts { get; set; }
public DbSet<PortfolioModel> Portfolios { get; set; }
public DbSet<SkillModel> Skills { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
And HomeController
public class HomeController : Controller
{
private CvContext db = new CvContext();
public ActionResult Index()
{
return View(db.Links.ToList());
}
}
Index.cshtml
@model IEnumerable<MvcCv.Models.LinkModel>
<ul>
@foreach (var item in Model)
{
<li>
<a href="@Html.DisplayFor(modelItem => item.LinkUrl)">
@Html.DisplayFor(modelItem => item.LinkName)
<span class="icon"></span>
<span class="menu-icon">
<img src="@Url.Content(item.LinkImage)" alt="" />
</span>
</a>
</li>
}
</ul>
How can i reach all models? I will use foreach for item in Model like Links. Thanks.
You should create a view model as follows:
Then from your controller populate them as to your requirements, as an example:
Then change the model in your view to
FooViewModel
and all your properties will be available in there.