在ASP.NET MVC中的上放置一个ID

时间:2022-12-21 03:22:58

I would like my views to be able to specify a class for the <body> tag, which lies in my master page.

我希望我的观点能够为标记指定一个类,它位于我的母版页中。

My first take was to do something like this:

我的第一步是做这样的事情:

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

However, that would require this in the master page, which doesn't work:

但是,这将需要在母版页中,这不起作用:

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

Any solutions to this?

对此有何解决方案?

4 个解决方案

#1


4  

I would suggest a different approach.

我建议采用不同的方法。

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

您可以从MasterModel开始创建视图模型的层次结构。实例化视图对象时,将体类传递给它。

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

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

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

Then in your master view which should be strongly typed to MasterView:

然后在您的主视图中应该强烈键入MasterView:

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

you just write:

你刚才写道:

<body class="<%= Model.BodyCss %>"></body>

#2


5  

In the layout you can do this on the body tag:

在布局中,您可以在body标签上执行此操作:

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

and then in your view you can do this:

然后在您的视图中,您可以这样做:

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

Edit: I also had to do this working with VB.NET and WebForms today and found a handy link for achieving the equivalent

编辑:我今天也必须使用VB.NET和WebForms,并找到了实现等效的方便链接

#3


4  

Why don't you do in your masterpage:

你为什么不在你的主页上做:

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

and then set ViewData["bodyClass"] in your Controller actions? That should be equivalent...

然后在Controller操作中设置ViewData [“bodyClass”]?那应该是等价的......

#4


4  

You could also specify the body id attribute in the View which wishes to set it:

您还可以在视图中指定要设置它的body id属性:

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

This helps to decouple the view from the controller, the controller (and/or view model) does not need to know about the id attribute of the body tag.

这有助于将视图与控制器分离,控制器(和/或视图模型)不需要知道body标签的id属性。

Set the id of the body tag in the master page like so:

在主页面中设置body标签的id,如下所示:

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

#1


4  

I would suggest a different approach.

我建议采用不同的方法。

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

您可以从MasterModel开始创建视图模型的层次结构。实例化视图对象时,将体类传递给它。

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

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

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

Then in your master view which should be strongly typed to MasterView:

然后在您的主视图中应该强烈键入MasterView:

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

you just write:

你刚才写道:

<body class="<%= Model.BodyCss %>"></body>

#2


5  

In the layout you can do this on the body tag:

在布局中,您可以在body标签上执行此操作:

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

and then in your view you can do this:

然后在您的视图中,您可以这样做:

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

Edit: I also had to do this working with VB.NET and WebForms today and found a handy link for achieving the equivalent

编辑:我今天也必须使用VB.NET和WebForms,并找到了实现等效的方便链接

#3


4  

Why don't you do in your masterpage:

你为什么不在你的主页上做:

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

and then set ViewData["bodyClass"] in your Controller actions? That should be equivalent...

然后在Controller操作中设置ViewData [“bodyClass”]?那应该是等价的......

#4


4  

You could also specify the body id attribute in the View which wishes to set it:

您还可以在视图中指定要设置它的body id属性:

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

This helps to decouple the view from the controller, the controller (and/or view model) does not need to know about the id attribute of the body tag.

这有助于将视图与控制器分离,控制器(和/或视图模型)不需要知道body标签的id属性。

Set the id of the body tag in the master page like so:

在主页面中设置body标签的id,如下所示:

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