3.1 添加View
Contorller的Active返回值是AcitionResult
用右键可以看,或者Inspector功能(有利于网页开发)
3.2 Razor
页面文件中插入代码
方式1:@{ var total = 7;}
方式2:<@total>
方式3:@{}
注释
*@
/* */
3.3 解析
layout模板
RenderBody()
网页刷新F5,Ctrl+F5清空缓存加载
传递数据
Controller到View
//简单
return View()
//复杂
ViewBag.Messag="122";
ViewBag.NumTimes=12;
return View();
//View
@ViewBag.Message
@{
ViewBag.NumTime
}
3.4 Rozor
//用括号显示本意
@(model.Name)
//@@方式表示显示一个@,表示转义
//不进行编码
@
{
string message=“<script>alert("Error")</script>”
}
@Html.Raw(message)
//防止XSS攻击
<script>
$(function)(){
var message="@Ajax.JavaScriptStringEncode(ViewBag.Username)"
}
</script>
//调用没有返回值的方法
@{Html.RenderPartial("SongPartial")}
语法
<span>1+2=@(1+2)</span>
//无编码
@Html.Raw(model.Message)
//注释
@*
*@
3.5 布局
布局页中有@RenderBody()用作占位
使用的时候在最开始
@{
layout="布局页位置"
ViewBag.Titla="Index"
}
//后面都是要显示的部分
也可以在布局页中写Boot
<footer>@RenderSection("Footer")</footer>
展示的时候必须指定
@section Footer{
This is the <strong>footer</strong>
}
不指定的方式,表示不是必须的,指定则显示,否则不显示
<footer>@RenderSection("Footer",required:false)</footer>
<footer>
@if(IsSectionDefiend("Footer")){
@RenderSection("Footer")}
else{
<span>default footer</span>
}
</footer>