【发布时间】:2014-03-02 04:52:21
【问题描述】:
我是 MVC 的新手,也是 Stackoverflow 的新手。我没有使用任何数据库,但我只想通过 2 个不同的控制器在 1 个视图上显示标题和按摩。我希望在我的 Index.cshtml 文件中显示来自 HomeController.cs 文件的 @ViewBag.Title 和来自 OtherController.cs 文件的 @ViewBag.Massage 显示。我该如何做到这一点以及如何将OtherController.cs 引用或附加到Index.cshtml?请不要告诉我,我要的是诸如如何将样式表附加到 html 之类的东西,但使用 MVC 是不可能的。
如果真的不可能,请告诉我如何通过具有 _Layout.cshtml 文件的部分视图来执行此操作。因为我不想在我的视图文件中写太多。
提前感谢您!
RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional
}
);
HomeController.cs
public ActionResult Index()
{
ViewBag.Title = "";
return View();
}
OtherController.cs
public ActionResult Index()
{
ViewBag.Massage = "";
return View();
}
Index.cshtml
<title>@ViewBag.Title</title>
<p>@ViewBag.Massage</p>
【问题讨论】:
标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing