gpt4 book ai didi

asp.net-mvc-3 - 如何在MVC3 View 页面中修改主体类

转载 作者:行者123 更新时间:2023-12-04 09:00:37 25 4
gpt4 key购买 nike

只是想在body标签中添加class =“myClass”。在MVC3 View 页面中是否有任何HTML帮助器或其他工具可以做到这一点?请指教,谢谢。

最佳答案

这与Aaron的解决方案非常相似,但是没有一节的权重(至少在我看来,这是针对比单个字符串更大的内容块的)。最简单的方法是使用ViewBag传递变量。

在您的布局中,只需打印出body标记的类,以及所有其他页面特定的变量(页面标题,额外的css/js脚本等)。

_Layout.cshtml:

<html>
<title>@ViewBag.Title</title>@* Can easily add in per page titles too *@
<body class="@ViewBag.BodyClass">
@RenderBody()
</body>
</html>

然后,您在 View 中设置的变量将向上传递到布局:

Index.cshtml:
@model MyViewModel
@{
ViewBag.Title = "This page title!";
ViewBag.BodyClass = "...";
}

关于asp.net-mvc-3 - 如何在MVC3 View 页面中修改主体类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6313230/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com