您的位置:首页 > 其它

mvc3 razor语法学习

2011-07-01 15:51 274 查看
1.多行声明:

@{

//cs代码

}

@{
inta=1;
stringname="username:"+usename;
}

2.一行多变量声明

@(//cs代码)

<p>你最爱的:@("水果"+fruit)</p>

3.在多行代码块中,用<text>标签,显示的时候可以不显示任何标签

@if(true)
{
<text>
hello

todayis@Datetime.Now
</tex>

}
运行后的结果是:

hello

todayis201171

标签没有了.

4@:

@:提示模板引擎这一行为内容块输出,不用解释

5.页面布局section

///<paramname="section">sectionname</param>
///<paramname="required">子页面是否要重写这个section,默认是true</param>

@RenderSection("sectionname",required:false)
如果required:true则子页面一定要实现这个section,不然会出现运行时错误

还可以定义默认的局部布局内容,eg:

@if(IsSectionDefined("sectionname")){

  @RenderSection("sectionname")

}else{

//为默认内容

}

实现在母版页使用的section

@sectionSideBar{
  //自定义section内容

}

_ViewStart.cshtml为所有页面引用了通用模板页,在其它子页面不用再次引入

_ViewStart.cshtml里面的内容

@{
Layout="~/Views/Shared/_Layout.cshtml";
}

在其它页面就可以不用写了

默认所有的页面都使用了通用的模板,如果某个页面不想使用通用模板,则应该加上这个:

@{
Layout="";
}这样就可以摆脱通用模板的干扰了

6@**@为注释

@{stringa="<a>111</a>";}

想要输出html,可以三种方式:
@Html.Raw(a)
@MvcHtmlString.Create(a)
@{WriteLiteral(a);}

@(newHtmlString("<h1>asdfasd</h1>"))
@(Html.Encode("<h1>asdfasd</h1>"))

7helper

在项目下新建app_code文件夹,在app_code中新建一个视图文件Content.cshtml,删除里面的Code,并将原Layout.cshtml中的helper部分copy到Content.cshtml下,这是发现Url缺少引用声明。添加@usingSystem.Web.Mvc,并添加一个的helper参数
@helperScript(
string
scriptName,UrlHelperUrl)
可以这样调用了:

@Content.Script(
"jquery-1.5.1.min.js"
,Url)

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: