Asp.net mvc change main file from view

I need to add a class attribute to the body tag from a view file (.aspx), but the tag is in the main file. How can I access the body tag from a view?

+3
source share
2 answers

In the output, you can simply add a jQuery client script that will run after your page is inserted together:

$('body').addClass('yourClass');

Another method is to store class data in your controller, for example:

ViewData["MasterPageBodyClass"] = "yourClass";

Then in your view MasterPageyou can check for this and add it if it exists:

<%
    string bodyClass = "";
    if (ViewData["MasterPageBodyClass"] != null)
    {
        bodyClass = "class=\"" + ViewData["MasterPageBodyClass"].ToString() + "\"";
    }
%>
<body <%= bodyClass %>>

, , , , ViewData, .

0

, master class:

<body class='someOtherClass <asp:ContentPlaceHolder ID="BodyCssOverrides" runat="server" />' >

:

<asp:Content ContentPlaceHolderID="BodyCssOverrides" runat="server">yourBodyClass</asp:Content>

ViewData.

, masterPage - , , , - - placeHolder

0

Source: https://habr.com/ru/post/1763885/


All Articles