简体   繁体   English

剃刀asp.net mvc3的条件部分语法

[英]conditional section syntaxe with razor asp.net mvc3

I'm not really familiar with the razor view engine. 我对剃刀视图引擎不是很熟悉。 So, i have a problem with the syntaxe. 因此,我对语法有疑问。

I have "_layout.cshtml" : 我有“ _layout.cshtml”:

@if (IsSectionDefined("Slideshow"))
{
     @RenderSection("Slideshow");
}
else
{
     <p>blah blah blah</p>
}

And then, in the "default.cshtml" 然后,在“ default.cshtml”中

@if(@ViewBag.Article == null)
{
    <p>blabla</p>
}
else
{
    @section Slideshow
    {
        <ul><li>slide here</li></ul>
    }
}

So, here is my problem : If i let the @ before section, i have an error [line:27] telling me that i don't have to keep the @ inside a @if statement : 所以,这是我的问题:如果我让@之前的部分出现错误[line:27],告诉我我不必将@保留在@if语句中:

"Mot clé "section" inattendu après le caractère "@". Une fois à l'intérieur du code,
vous n'avez pas besoin de préfixer des constructions telles que "section" avec "@"."

If i don't let the @ before section, i have an error[line:29] 2 lines after ( 如果我不让@之前的部分出现错误[line:29](

  • ...) telling me a } is missing. ...)告诉我}丢失。

     CS1513: } attendue 

    So, i guess it is possible to do but i don't know how to do it. 所以,我想有可能做,但我不知道该怎么做。 Could you help me please. 请问你能帮帮我吗。

    Thanks 谢谢

  • this should compile: 这应该编译为:

    @if (IsSectionDefined("Slideshow"))
    {
         RenderSection("Slideshow");
    }
    else
    {
        @:<p>blah blah blah</p>
    }
    
    
    @if(ViewBag.Article == null)
    {
        @:<p>blabla</p>
    }
    else
    {
        section Slideshow
        {
            @:<ul><li>slide here</li></ul>
        }
    }
    

    You need to start ac# statement with a @ only if the compiler doesn't know you want to write actual code. 仅当编译器不知道您要编写实际代码时,才需要使用@开头ac#语句。

    Using @: at the start of plain text, the compiler knows its plain text. 在纯文本开头使用@ :,编译器知道其纯文本。

    Using <text></text> tags to enclose text has the same effect. 使用<text></text>标签将文本括起来具有相同的效果。 Every c# statement inside </text> tag needs to start with a @ </text>标记内的每个c#语句都必须以@开头

    You can do like this, 你可以这样

     @{
        if(ViewBag.Article == null)
    {
        <p>blabla</p>
    }
    else
    {
        section Slideshow
        {
            <ul><li>slide here</li></ul>
        }
    }    
        }
    

    声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM