Pro Aspnet MVC 4读书笔记(4) - Working with Razor
Listing 5-1. Creating a Simple Domain Model Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Razor.Models
{
public class Product
{
public int ProductId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { set; get; }
}
}
Listing 5-2. A Simple Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Razor.Models; namespace Razor.Controllers
{
public class HomeController : Controller
{
Product prod = new Product {
ProductId = ,
Name = "Kayak",
Description = "A boat for one person",
Category = "Watersports",
Price = 275M
}; public ActionResult Index()
{
return View(prod);
}
}
}
Listing 5-3. A Simple Razor View
@model Razor.Models.Product @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div> </div>
</body>
</html>
Listing 5-4. Referring to a View Model Object Property in a Razor View
@model Razor.Models.Product @{
Layout = null;
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Model.Name
</div>
</body>
</html>
Listing 5-5. The Initial Contents of a Layout
<!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>@ViewBag.Title</title>
</head>
<body>
<div>
@RenderBody()
</div>
</body>
</html>
Listing 5-6. Adding Elements to a Layout
<!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:solid medium black;font-size:20pt">
@RenderBody()
</div>
<h2>Visit <a href="http://apress.com">Appress</a></h2>
</body>
</html>
Listing 5-7. Using the Layout Property to Specify a View File
@model Razor.Models.Product @{
ViewBag.Title = "Product Name";
Layout = "~/Views/_BasicLayout.cshtml";
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Model.Name
</div>
</body>
</html>
Listing 5-8. Creating a View Start File
@{
Layout = "~/Views/_BasicLayout.cshtml";
}
Listing 5-9. Updating the View to Reflect the Use of a View Start File
@model Razor.Models.Product @{
ViewBag.Title = "Product Name";
} <!DOCTYPE html> <html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<div>
@Model.Name
</div>
</body>
</html>
Listing 5-10. Adding a New Action Method to the Home Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Razor.Models; namespace Razor.Controllers
{
public class HomeController : Controller
{
Product prod = new Product {
ProductId = ,
Name = "Kayak",
Description = "A boat for one person",
Category = "Watersports",
Price = 275M
}; public ActionResult Index()
{
return View(prod);
} public ActionResult NameAndPrice()
{
return View(prod);
}
}
}
Listing 5-11. The Contents of the NameAndPrice View
@model Razor.Models.Product @{
ViewBag.Title = "NameAndPrice";
Layout = "~/Views/_BasicLayout.cshtml";
} <h2>NameAndPrice</h2>
Listing 5-12. Adding to the NameAndPrice Layout
@model Razor.Models.Product @{
ViewBag.Title = "NameAndPrice";
Layout = "~/Views/_BasicLayout.cshtml";
} <h2>NameAndPrice</h2>
The product is @Model.Name and it costs $@Model.Price
Listing 5-13. The DemoExpression Action Method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Razor.Models; namespace Razor.Controllers
{
public class HomeController : Controller
{
Product prod = new Product {
ProductId = ,
Name = "Kayak",
Description = "A boat for one person",
Category = "Watersports",
Price = 275M
}; public ActionResult Index()
{
return View(prod);
} public ActionResult NameAndPrice()
{
return View(prod);
} public ActionResult DemoExpression()
{
ViewBag.ProductCount = ;
ViewBag.ExpressShip = true;
ViewBag.ApplyDiscount = false;
ViewBag.Supplier = null; return View(prod);
}
}
}
Listing 5-14. The Contents of the DemoExpression View File
@model Razor.Models.Product @{
ViewBag.Title = "DemoExpression";
} <table>
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Price</td>
<td>@Model.Price</td>
</tr>
<tr>
<td>Stock Level</td>
<td>@ViewBag.ProductCount</td>
</tr>
</tbody>
</table>
Listing 5-15. Using a Razor Expression to Set an Attribute Value
@model Razor.Models.Product @{
ViewBag.Title = "DemoExpression";
Layout = "~/Views/_BasicLayout.cshtml";
} <table>
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Price</td>
<td>@Model.Price</td>
</tr>
<tr>
<td>Stock Level</td>
<td>@ViewBag.ProductCount</td>
</tr>
</tbody>
</table> <div data-discount="@ViewBag.ApplyDiscount" data-express="@ViewBag.ExpressShip"
data-supplier="@ViewBag.Supplier">
The Containing element has data attributes
</div> Discount:<input type="checkbox" checked="@ViewBag.ApplyDiscount" />
Express: <input type="checkbox" checked="@ViewBag.ExpressShip" />
Supplier:<input type="checkbox" checked="@ViewBag.Supplier" />
Listing 5-16. Using a Conditional Razor Statement
@model Razor.Models.Product @{
ViewBag.Title = "DemoExpression";
Layout = "~/Views/_BasicLayout.cshtml";
} <table>
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Price</td>
<td>@Model.Price</td>
</tr>
<tr>
<td>Stock Level</td>
<td>
@switch ((int)ViewBag.ProductCount)
{
case 0:
@: Out of Stock
break;
case 1:
<b>Low Stock(@ViewBag.ProductCount)</b>
break;
default:
@ViewBag.ProductCount
break;
}
</td>
</tr>
</tbody>
</table>
Listing 5-17. Using an if Statement in a Razor View
@model Razor.Models.Product @{
ViewBag.Title = "DemoExpression";
Layout = "~/Views/_BasicLayout.cshtml";
} <table>
<thead>
<tr>
<th>Property</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td>@Model.Name</td>
</tr>
<tr>
<td>Price</td>
<td>@Model.Price</td>
</tr>
<tr>
<td>Stock Level</td>
<td>
@if (ViewBag.ProductCount == 0)
{
@: Out of Stock
}
else if (ViewBag.ProductCount == 1)
{ <b>Low Stock(@ViewBag.ProductCount)</b>
}
else
{
@ViewBag.ProductCount
}
</td>
</tr>
</tbody>
</table>
Listing 5-18. The DemoArray Action Method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Razor.Models; namespace Razor.Controllers
{
public class HomeController : Controller
{
Product prod = new Product {
ProductId = ,
Name = "Kayak",
Description = "A boat for one person",
Category = "Watersports",
Price = 275M
}; // Other action methods omitted for brevity public ActionResult DemoArray()
{
Product[] array =
{
new Product {Name = "Kayak", Price = 275M},
new Product {Name = "Lifejacket", Price = 48.95M},
new Product {Name = "Soccer ball", Price = 19.50M},
new Product {Name = "Corner flag", Price = 34.95M}
}; return View(array);
}
}
}
Listing 5-19. The Contents of the DemoArray.html File
@model Razor.Models.Product[] @{
ViewBag.Title = "DemoArray";
Layout = "~/Views/_BasicLayout.cshtml";
} @if (Model.Length > 0)
{
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach (Razor.Models.Product prod in Model)
{
<tr>
<td>@prod.Name</td>
<td>$@prod.Price</td>
</tr>
}
</tbody>
</table>
}
else
{
<h2>No product data</h2>
}
Listing 5-20. Applying the @using Expression
@using Razor.Models
@model Product[] @{
ViewBag.Title = "DemoArray";
Layout = "~/Views/_BasicLayout.cshtml";
} @if (Model.Length > 0)
{
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach (Razor.Models.Product prod in Model)
{
<tr>
<td>@prod.Name</td>
<td>$@prod.Price</td>
</tr>
}
</tbody>
</table>
}
else
{
<h2>No product data</h2>
}
Pro Aspnet MVC 4读书笔记(4) - Working with Razor的更多相关文章
- Pro Aspnet MVC 4读书笔记(1) - Your First MVC Application
Listing 2-1. The default contents of the HomeController class using System; using System.Collections ...
- Pro Aspnet MVC 4读书笔记(3) - Essential Language Features
Listing 4-1. The Initial Content of the Home Controller using System; using System.Collections.Gener ...
- Pro Aspnet MVC 4读书笔记(2) - The MVC Pattern
Listing 3-1. The C# Auction Domain Model using System; using System.Collections.Generic; using Syste ...
- Pro Aspnet MVC 4读书笔记(5) - Essential Tools for MVC
Listing 6-1. The Product Model Class using System; using System.Collections.Generic; using System.Li ...
- 【Tools】Pro Git 一二章读书笔记
记得知乎以前有个问题说:如果用一天的时间学习一门技能,选什么好?里面有个说学会Git是个很不错选择,今天就抽时间感受下Git的魅力吧. Pro Git (Scott Chacon) 读书笔记: ...
- [Git00] Pro Git 一二章读书笔记
记得知乎以前有个问题说:如果用一天的时间学习一门技能,选什么好?里面有个说学会Git是个很不错选择,今天就抽时间感受下Git的魅力吧. Pro Git (Scott Chacon) 读书笔记: ...
- 《Pro Android Graphics》读书笔记之第四节
Android Procedural Animation: : XML, Concepts and Optimization Procedural Animation Concepts: Tweens ...
- 《Pro Android Graphics》读书笔记之第三节
Android Frame Animation: XML, Concepts and Optimization Frame Animation Concepts: Cels, Framerate, a ...
- 《Pro Android Graphics》读书笔记之第六节
Android UI Layouts: Graphics Design Using the ViewGroup Class Android ViewGroup Superclass: A Founda ...
随机推荐
- 远程方法调用(RMI)原理与示例 (转)
RMI介绍 远程方法调用(RMI)顾名思义是一台机器上的程序调用另一台机器上的方法.这样可以大致知道RMI是用来干什么的,但是这种理解还不太确切.RMI是Java支撑分布式系统的基石,例如著名的EJB ...
- wpf 模拟3D效果(和手机浏览图片效果相似)(附源码)
原文 wpf 模拟3D效果(和手机浏览图片效果相似)(附源码) pf的3D是一个很有意思的东西,类似于ps的效果,类似于电影动画的效果,因为动画的效果,(对于3D基础的摄像机,光源,之类不介绍,对于依 ...
- Java 并发专题 : CyclicBarrier 打造一个安全的门禁系统
继续并发专题~ 这次介绍CyclicBarrier:看一眼API的注释: /** * A synchronization aid that allows a set of threads to all ...
- android于src和background差额
ImageView中XML属性src和background的差别: background会依据ImageView组件给定的长宽进行拉伸,而src就存放的是原图的大小.不会进行拉伸.src是图片内容(前 ...
- 【原创】纯OO:从设计到编码写一个FlappyBird (三)
第二部分请点这里 下面首先来实现Bing接口! 实现Bing接口的类取名SimpleBing. 容易发现,SimpleBing类总的来说要向下,但点击一下又得向上,向上到了一定界限又得向下,但我们又只 ...
- Linux(Centos)中tcpdump参数用法详解(转)
在linux下进行编程开发的人尤其是网络编程的人会经常需要分析数据包,那么一定会用到tcpdump,下面就是关于tcpdump的使用方法说明(1). tcpdump的选项 -a 将网络地址 ...
- 认识mongoDB数据库
mongodb中有三元素:数据库,集合,文档,其中“集合”对应关系型数据库中的“表”,“文档”对应“行”. 安装mongoDB: 去官网下载对应系统的mongoDB压缩包,解压后将文件夹重命名为mon ...
- JDBC连接数据库 prepareStatement
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import ...
- 1.网络工具:ifconfig,ping,netstate,Redhat命令和图形化设置ip,finger,nslookup
1 ip ad查看网卡编号 2.ifconfig查看网卡信息 3.关闭网卡 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdG90b3R1enVvcX ...
- C#设计及其UML(反向工程)
OOP之C#设计及其UML(反向工程) 现在总结一下C#类关键字(virtual.abstract.override.new.sealed)的使用(以C#代码体现),并再次熟悉一下OOP思想,使用 ...