代码之家  ›  专栏  ›  技术社区  ›  jjnguy Julien Chastang

如何基于Asp.NETMVC中的视图设置<body>类?

  •  2
  • jjnguy Julien Chastang  · 技术社区  · 14 年前

    我想定下目标 <body> 基于视图的类。

    我读过几篇关于大力键入母版页的帖子,但似乎没有一篇是完全合适的,或者至少我不知道如何编写它。

    我已经注意到,魔兽世界做到了这一点,所以这是可能的。

    获取错误: Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: CS1061: 'System.Web.Mvc.HtmlHelper' does not contain a definition for 'Body' and no extension method 'Body' accepting a first argument of type 'System.Web.Mvc.HtmlHelper' could be found (are you missing a using directive or an assembly reference?) body

    namespace MySite.Helpers
    {
        public static class HtmlHelpers
        {
            public static MvcHtmlString Body(this HtmlHelper htmlHelper)
            {
                string currentControllerName = (string)htmlHelper.ViewContext.RouteData.Values["controller"];
                string currentActionName = (string)htmlHelper.ViewContext.RouteData.Values["action"];
                string css = currentControllerName + "-" + currentActionName;
                var body = new TagBuilder("body");
                body.AddCssClass(css);
                return MvcHtmlString.Create(body.ToString(TagRenderMode.StartTag));
            }
        }
    
    }
    

    ~/Views/Shared/Site.Master:

    <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
        <link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
    </head>
    
    <body id="argh" class="<%= Html.Body() %>">
    
    2 回复  |  直到 14 年前
        1
  •  3
  •   Darin Dimitrov    14 年前

    可以使用助手方法:

    public static MvcHtmlString Body(this HtmlHelper htmlHelper)
    {
        string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
        string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
        string css = GetCss(currentControllerName, currentActionName);
        var body = new TagBuilder("body");
        body.AddCssClass(css);
        return MvcHtmlString.Create(body.ToString(TagRenderMode.StartTag));
    }
    

    然后在母版页中:

    <%= Html.Body() %>
        ...
    </body>
    
        2
  •  0
  •   Gaff    8 年前

    您可以在布局cshtml文件中执行以下操作:

    <body class="@HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString().ToLower() @HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString().ToLower()-@HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString().ToLower()">
    

    这将导致:

    <body class="controller controller-action">