Entering an identifier in <body> in ASP.NET MVC

I would like my views to be able to indicate the class for the tag <body>that is on my main page.

My first step was to do something like this:

<asp:Content ContentPlaceHolderID="BodyClassContent" runat="server">
    view-item</asp:Content>

However, this will require this on the main page, which does not work:

<body class="<asp:ContentPlaceHolder ID="BodyClassContent" runat="server" />">

Any solutions for this?

+3
source share
4 answers

I suggest a different approach.

You create a hierarchy of view models starting with MasterModel. When you instantiate the view object, you pass the body class to it.

public class MasterModel
{
    string BodyCss { get; set; }

    public MasterModel (string bodyCss)
    {
        BodyCss = bodyCss;
    }
}

public class MyView1Model : MasterModel
    : base ("body-view1")
{
}

Then in your main view, which should be strictly typed for MasterView:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<MasterModel>" %>

you just write:

<body class="<%= Model.BodyCss %>"></body>
+4
source

body:

<body @RenderSection("BodyAttributes", false)>

, , :

@section BodyAttributes {
    id="login" class="login"
}

: VB.NET WebForms

+5

:

<body class="<%=ViewData["bodyClass"].toString()%>">

ViewData [ "bodyClass" ] ? ...

+4

id , :

@{
ViewBag.Title = "Test";
ViewData["BodyID"] = "test";
Layout = "~/Views/Shared/_Layout.cshtml";}

, (/ ) id body.

body :

<body id="@ViewData["BodyID"]">
+4

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


All Articles