1、引用对象模型
public ActionResult Index()
{
Product p = new Product();
p.Name = "ball";
return View(p);
}
@model Razor.Models.Product
如果一个视图页是一个强类型视图,那么就会在该视图页的最上方对需要的视图模型进行引用
2、使用视图模型
<div> @Model.Name </div>
3、使用视图包
在控制器中,先给视图包赋值 public Action Index(){ ViewBag.ApplyDiscount=false; } 在视图页中使用 <div data-discount="@ViewBag.ApplyDiscount"> @ViewBag.ApplyDiscount </div>
4、使用布局页
在Views文件夹内创建 _BasicLayout.cshtml 和 _ViewStart.cshtml 文件
@{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>@ViewBag.title</title> </head> <body> <h1> Product Information </h1> <div style="padding: 20px; border: 1px solid black; font-size: 20pt;"> @RenderBody() @*引用该布局页的视图都会填充在这个div标签内*@ </div> <div> <h2> Visit<a href="http://apress.com">Apress</a> </h2> </div> </body> </html>