C# 如何在主视图/索引视图MVC中显示两个ActionResult#

C# 如何在主视图/索引视图MVC中显示两个ActionResult#,c#,asp.net-mvc,C#,Asp.net Mvc,我有两个控制器 BloggsController: //Last blogg from the database public ActionResult LastBlogg() { var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1); return View(lastblogg); } //Last recipe

我有两个控制器

BloggsController:

   //Last blogg from the database
        public ActionResult LastBlogg()
        {
            var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);

            return View(lastblogg);
        }
//Last recipe from the database
public ActionResult LastRecipe()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
//Last recipe from the database
public ActionResult Index()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
ViewData.Model = new MyCustomViewData
{
Dish = db.Dishes.OrderByDescending(o => o.ID).Take(1);
Blog = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
}

return View();
洗碗机控制器:

   //Last blogg from the database
        public ActionResult LastBlogg()
        {
            var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);

            return View(lastblogg);
        }
//Last recipe from the database
public ActionResult LastRecipe()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
//Last recipe from the database
public ActionResult Index()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
ViewData.Model = new MyCustomViewData
{
Dish = db.Dishes.OrderByDescending(o => o.ID).Take(1);
Blog = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
}

return View();
我想在我的起始页Views/Home/index上显示结果

如果我把它放在HomeController中:

   //Last blogg from the database
        public ActionResult LastBlogg()
        {
            var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);

            return View(lastblogg);
        }
//Last recipe from the database
public ActionResult LastRecipe()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
//Last recipe from the database
public ActionResult Index()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
ViewData.Model = new MyCustomViewData
{
Dish = db.Dishes.OrderByDescending(o => o.ID).Take(1);
Blog = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
}

return View();

我可以在我的起始页上显示配方的结果,但如何在om起始页上同时显示blogg和配方的结果?

创建一个视图模型,并将blogg和配方添加到其中

public ActionResult Index()
{
    var lastRecipe = db.Dishes.OrderByDescending(o => o.ID).Take(1);
    var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);

   var model = new BloggRecipeModel(lastRecipe, lastblogg);

   return View(model);

}创建一个视图模型,并将Blogg和Recipe添加到其中

public ActionResult Index()
{
    var lastRecipe = db.Dishes.OrderByDescending(o => o.ID).Take(1);
    var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);

   var model = new BloggRecipeModel(lastRecipe, lastblogg);

   return View(model);

}

您只需在Models文件夹中创建自定义ViewData,如下所示:

public class MyCustomViewData
{
public Dish Dish {get;set;}
public Blog Blog {get;set;}
}
然后在控制器中:

   //Last blogg from the database
        public ActionResult LastBlogg()
        {
            var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);

            return View(lastblogg);
        }
//Last recipe from the database
public ActionResult LastRecipe()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
//Last recipe from the database
public ActionResult Index()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
ViewData.Model = new MyCustomViewData
{
Dish = db.Dishes.OrderByDescending(o => o.ID).Take(1);
Blog = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
}

return View();

在视图中,将@Model属性设置为Models.MyCustomViewData并进行相应处理。

您只需在Models文件夹中创建自定义ViewData,如下所示:

public class MyCustomViewData
{
public Dish Dish {get;set;}
public Blog Blog {get;set;}
}
然后在控制器中:

   //Last blogg from the database
        public ActionResult LastBlogg()
        {
            var lastblogg = db.Bloggs.OrderByDescending(o => o.ID).Take(1);

            return View(lastblogg);
        }
//Last recipe from the database
public ActionResult LastRecipe()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
//Last recipe from the database
public ActionResult Index()
{
    var last = db.Dishes.OrderByDescending(o => o.ID).Take(1);

    return View(last);
}
ViewData.Model = new MyCustomViewData
{
Dish = db.Dishes.OrderByDescending(o => o.ID).Take(1);
Blog = db.Bloggs.OrderByDescending(o => o.ID).Take(1);
}

return View();

在您的视图中,将@Model属性设置为Models.MyCustomViewData并进行相应处理。

您应该为
LastBlogg
LastRecipe
创建单独的局部视图,并将它们都放在您的主页上(需要新的模型).

您应该为
LastBlogg
LastRecipe
创建单独的局部视图,并将它们放在您的主页上(需要新型号)