Razor视图引擎的基础语法

1. 使用逻辑处理

@{
    if (xx)
    {
    //do something
    }
    else
    {
    //do anything
    }
}

2. 在@{... }内部使用html标记

@{
    <p>text</P>
    <div>div1</div>
}

3. 在@{...}内部输出文本
利用@:进行单行输出:

@{
    @:This is some text
    @:This is text too
    @:@i 也可输出变量
}

利用<text />进行多行输出:

@{
    <text>
        tomorrow is good
        some girl is nice
    </text>
}

4. 在@{...}内部使用注释

@{
    //单行注释
    var i = 10;
    //defg
}
 
    @* 多行注释 *@
    @* 
        多行注释
        多行注释 
    *@
 
 
@{
    @*
        多行注释
        多行注释 
    *@
    var i = 10;  @* asdfasf *@
}
 
<!-- 同时也可以使用C#默认的/* ... */ -->
 
@{
    /*
        多行注释 
    */
}

若在@{ ... }内部使用<!-- -->注释,则会输出到页面之中,如果在<!-- -->内部使用@变量,则会被处理
@{

<!-- time now: @DateTime.Now.ToString() -->
}
输出: <!-- time now: 4/9/2011 12:01 -->

5. 类型转换
AsInt(), IsInt()
AsBool(),IsBool()
AsFloat(),IsFloat()
AsDecimal(),IsDecimal()
AsDateTime(),IsDateTime()
ToString()
例子:

@{
    var i = “10”;
}
 
<p> i = @i.AsInt() </p> <!-- 输出 i = 10 --> 

6. 使用循环

<!--方式1-->
@for (int i = 10; i < 11; i++)
{
    @:@i
}
<!--方式2-->
@{
    for (int i = 10; i < 11; i++)
    {
        //do something
    }
}
 
<!--while同理-->

 

posted on 2013-08-12 15:17  wuzx-blog  阅读(208)  评论(0编辑  收藏  举报