从视图asp.net的MVC修改主文件
我需要class属性从视图文件(的.aspx)添加到body标签,但标签是主文件英寸我怎样才能从视图访问body标签?
I need to add class attribute to the body tag from a view file (.aspx), but the tag is in the master file. How can I access the body tag from a view?
在您查看输出,你可以只添加一个 jQuery的客户端脚本来做到这一点,这将运行一次你的网页是拼凑起来的:
In your view output you could just add a jQuery client script to do it which will run once your page is pieced together:
$('body').addClass('yourClass');
另一种方法是将类数据存储在控制器中,如:
Another method would be to store the class data in your controller like:
ViewData["MasterPageBodyClass"] = "yourClass";
然后在你的母版
查看您可以检查这是否存在等,并添加它,如果它存在:
Then in your MasterPage
view you could check for the existance of this and add it if it exists:
<%
string bodyClass = "";
if (ViewData["MasterPageBodyClass"] != null)
{
bodyClass = "class=\"" + ViewData["MasterPageBodyClass"].ToString() + "\"";
}
%>
<body <%= bodyClass %>>
只有所需的类被连接到主体实际上需要将类存储在的ViewData code>每隔一个行动可以忽略它的控制器的动作。
Only the controller actions that required the class to be attached to the body would actually need to store the class in the ViewData
every other action could just ignore it.