我想在一个视图中使用两个模型,并查看父视图模型。
我有两种模式:
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;
}
}
}
和
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; }
}
这是我的父视图模型:
public class DishBlogg
{
public Blogg Blogging { get; set; }
public Dish Dishing { get; set; }
}
如何在我的视图中显示结果(Home / index.cshtml)?这不起作用:
@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)
}
我可以用这个(当然)展示一个模型:
@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)
}
但是如何显示两者(使用DishBlogg)?
我收到此错误:传入字典的模型项类型为'System.Data.Entity.Infrastructure.DbQuery 1[XXXX.Models.Blogg]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1 [XXXX.Models.DishBlogg]'
编辑:
这是HomeController的索引,我从数据库中挑选出最后一篇文章。我想对菜一样做,并在同一个视图中呈现
public ActionResult Index()
{
var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
return View(lastblogg);
}
答案 0 :(得分:2)
您需要调整视图模型:
public class DishBlogg
{
public IEnumerable<Blogg> Blogging { get; set; }
public IEnumerable<Dish> Dishing { get; set; }
}
然后尝试添加.ToList()
调用以急切执行控制器中的查询:
public ActionResult Index()
{
var model = new DishBlogg
{
Blogging = db.Bloggs.OrderByDescending(o => o.ID).Take(1).ToList(),
Dishing = db.Dishes.OrderByDescending(o => o.ID).Take(1).ToList(),
}
return View(model);
}
然后在你看来:
@model XXXXXX.Models.DishBlogg
@foreach (var blog in Model.Blogging)
{
...
}
@foreach (var blog in Model.Dishing)
{
...
}
答案 1 :(得分:0)
我认为你的问题很明确。
您的模型是DishBlog,但您传递的是Blogg
试试这个:
public ActionResult Index()
{
var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1).FirstOrDefault();
var list = new List<DishBlogg>();
list.Add(new DishBlogg(){Blog = lastblog});
return View(list);
}
答案 2 :(得分:0)
如果要将viewmodel“DishBlogg”传递给视图,则需要从视图中的模型声明中删除IEnumerable
在您的视图中,您使用@model IEnumerable<XXXXXX.Models.DishBlogg>
表示View预期的模型属于DishBlogg的IEnumerable类型。
但是在控制器中,您将一个DishBlogg类型的对象作为模型返回View(viewModel);因此错误。
将视图更改为@model XXXXXX.Models.DishBlogg
或更改您的视图模型以返回列表并将IEnumerable的DishBlogg作为模型传递给控制器中的视图
所以而不是@model IEnumerable<XXXXXX.Models.DishBlogg>
使用此@model <XXXXXX.Models.DishBlogg>
将您的viewmodel类更改为此
public class DishBlogg(Blogg blogg, Dish dish)
{
this.LastBlogg = blogg;
this.LastDish = dish;
}
public Blogg LastBlogg { get; private set; }
public Dish LastDish { get; private set; }
将视图模型填充并传递到视图中
public ActionResult Index()
{
var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
var lastdish = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
var viewModel = new DishBlogg(lastblogg , lastdish );
return View("Index", viewModel);
}