asp.net-mvc-3 – 剃刀引擎 – 如何根据不同的条件渲染不同的主体标签?

前端之家收集整理的这篇文章主要介绍了asp.net-mvc-3 – 剃刀引擎 – 如何根据不同的条件渲染不同的主体标签?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我现在的代码
@{
    if (Request.Browser.Browser == "IE") {
        if (Request.Browser.MajorVersion == 7) { <body class="ie7"> }
        if (Request.Browser.MajorVersion == 8) { <body class="ie8"> }
        if (Request.Browser.MajorVersion == 9) { <body class="ie9"> }
        if (Request.Browser.MajorVersion > 9) { <body> }
    } else {
        <body>
    }
}

这是浏览器尝试呈现时返回的错误

Parser Error Message: The code block is missing a closing “}”
character. Make sure you have a matching “}” character for all the
“{” characters within this block,and that none of the “}” characters
are being interpreted as markup.

有没有搞错?我能够在标准的ASP.NET模板语法中做到这一点!这就是这样的:

<% // Adaptation of paulirish.com/2008/conditional-stylesheets-vs-css-hacks-answer-neither/
   if ( (Request.Browser.Browser == "IE") && (Request.Browser.MajorVersion == 7) ) { %><body class="ie7"><% } %>
<% if ( (Request.Browser.Browser == "IE") && (Request.Browser.MajorVersion == 8) ) { %><body class="ie8"><% } %>
<% if ( (Request.Browser.Browser == "IE") && (Request.Browser.MajorVersion == 9) ) { %><body class="ie9"><% } %>
<% if ( (Request.Browser.Browser == "IE") && (Request.Browser.MajorVersion > 9) ) { %><body><% } %>
<% if   (Request.Browser.Browser != "IE") { %><body><% } %>

解决方法

更好的选择可能是在视图顶部声明一个ieClass变量,然后在body标签中引用它.
@{
    string ieClass = "";
    if (Request.Browser.Browser == "IE") {
        if (Request.Browser.MajorVersion == 7) { ieClass="ie7"; }
        else if (Request.Browser.MajorVersion == 8) { ieClass="ie8"; }
        else if (Request.Browser.MajorVersion == 9) { ieClass="ie9"; }
    }
}

<body class="@ieClass">

猜你在找的asp.Net相关文章