从视图asp.net的MVC修改主文件 [英] asp.net mvc modifying master file from a view

查看:64
本文介绍了从视图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 每隔一个行动可以忽略它的控制器的动作。

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.

这篇关于从视图asp.net的MVC修改主文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆