I want to use two models in one view and have looked at parent view model.
I have two models:
public class Blogg
{
public int ID { get; set; }
public string Blogg_name { get; set; }
public string Message { get; set; }
public string Blogg_subject { get; set; }
private DateTime _Blogg_date = DateTime.Now;
public DateTime Blogg_date
{
get
{
return _Blogg_date;
}
set
{
_Blogg_date = value;
}
}
}
And
public class Dish
{
public int ID { get; set; }
public string Title { get; set; }
public string MainIngredient { get; set; }
public string Cooking { get; set; }
public string Drink { get; set; }
public string Ingredient { get; set; }
public string Type { get; set; }
}
This is my parent view model:
public class DishBlogg
{
public Blogg Blogging { get; set; }
public Dish Dishing { get; set; }
}
How do I show the result in my view (Home/index.cshtml)? This is not working:
@model IEnumerable<XXXXXX.Models.DishBlogg>
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.Blogging.Blogg_date)
@Html.DisplayFor(modelItem => item.Blogging.Blogg_subject)
@Html.DisplayFor(modelItem => item.Blogging.Blogg_name)
@Html.DisplayFor(modelItem => item.Blogging.Message)
}
I can show one model with this (of course):
@model IEnumerable<XXXXXX.Models.Blogg>
@foreach (var item in Model)
{
@Html.DisplayFor(modelItem => item.Blogg_date)
@Html.DisplayFor(modelItem => item.Blogg_subject)
@Html.DisplayFor(modelItem => item.Blogg_name)
@Html.DisplayFor(modelItem => item.Message)
}
But how do I show both (with DishBlogg)?
I get this error: The model item passed into the dictionary is of type 'System.Data.Entity.Infrastructure.DbQuery1[XXXX.Models.Blogg]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[XXXX.Models.DishBlogg]'
Edit:
Here is the Index of HomeController where I pick out the last post from the database. I want to do the same with the dish and present both at the same view
public ActionResult Index()
{
var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
return View(lastblogg);
}