我从事 mvc 5 项目。
在我的 _Layout.cshtml
我有一个名为 id="topBar"
的 html 元素此元素显示在每个页面中。
但是我有一个名为 About.cshtml 的页面我不希望这个页面只显示元素 id="topBar"
_Layout.cshtml
中的所有其他 html 元素我希望它们在 About.cshtml
中正常显示.
知道如何实现上述外观吗?
最佳答案
实现此目的的一种简单方法(不使用 javascript)是在 ViewBag
中设置一个值。在您的左右Action
并在这样的布局中使用它:
_Layout.cshtml
@if (ViewBag.ShowTopBar ?? false)
{
<div id="topBar">
</div>
}
您的内部操作 Controller :
public class MyAwesomeController : Controller
{
public ActionResult About()
{
ViewBag.ShowTopBar = true;
return View();
}
}
关于asp.net - 如何从_Layout.cshtml页面隐藏页面上的特定元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42389341/