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 ...
随机推荐
- Zookeeper实践方案:(4)命名服务
1.基本介绍 命名服务是指通过指定的名字来获取资源或者服务的地址,提供者的信息.利用Zookeeper非常easy创建一个全局的路径,而这个路径就能够作为一个名字.它能够指向集群中的集群.提供的服务的 ...
- git 常用命令及问题解决(转)
git init 产生的目录解释error: src refspec master does not match any.引起该错误的原因是,目录中没有文件,空目录是不能提交上去的error: ins ...
- Haskell 几乎无疼痛入门指南
当他重装Linux 机会虚拟机,安装 haskell 录制的过程中有什么.的方式来帮助那些谁在徘徊haskell进入外读者. 基本概念: Haskell : 是一门通用函数式语言.差点儿能够进行不论什 ...
- APK 代码混淆
# To enable ProGuard in your project, edit project.properties # to define the proguard.config proper ...
- async And await异步编程活用基础
原文:async And await异步编程活用基础 好久没写博客了,时隔5个月,奉上一篇精心准备的文章,希望大家能有所收获,对async 和 await 的理解有更深一层的理解. async 和 a ...
- Jquery简介选择的
前言 Jquery一个js相框(程序代码相结合)这是一个程序开发过程中的半成品:分类似该框架EXTJS. 依赖库:jquery-XXX.js 语法:$() 正文 5择器 id选择器 $("# ...
- 【Web探索之旅】第一部分:什么是Web?
内容简介 1.Web探索之旅:开宗明义 2.第一部分第一课:什么是Web? 3.第一部分第二课:Web,服务和云 4.第一部分第三课:Web的诞生史 Web探索之旅:开宗明义 大家好. 我们这个系列课 ...
- Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies
B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...
- 矢量编程——随着MNIST案例
矢量编程使用的所有明确的矢量运算,而不是for周期. 上一节所用的是512*512*10的数据集非常小.我们取的patch非常小(8*8),学来的特征非常少(25).而我又凝视掉了梯度校验(偷懒),所 ...
- Nexon由Xsolla全球支付服务
韩国游戏公司纳克森决Nexon定从今年10月1日起,与Xsolla开展Playspan的合作,向全球提供更好的服务. 当Nexon的玩家随时想购买NX点数的时候.Xsolla的服务能够进入程序,让您的 ...