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的更多相关文章

  1. Pro Aspnet MVC 4读书笔记(1) - Your First MVC Application

    Listing 2-1. The default contents of the HomeController class using System; using System.Collections ...

  2. Pro Aspnet MVC 4读书笔记(3) - Essential Language Features

    Listing 4-1. The Initial Content of the Home Controller using System; using System.Collections.Gener ...

  3. Pro Aspnet MVC 4读书笔记(2) - The MVC Pattern

    Listing 3-1. The C# Auction Domain Model using System; using System.Collections.Generic; using Syste ...

  4. 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 ...

  5. 【Tools】Pro Git 一二章读书笔记

    记得知乎以前有个问题说:如果用一天的时间学习一门技能,选什么好?里面有个说学会Git是个很不错选择,今天就抽时间感受下Git的魅力吧.   Pro Git (Scott Chacon) 读书笔记:   ...

  6. [Git00] Pro Git 一二章读书笔记

    记得知乎以前有个问题说:如果用一天的时间学习一门技能,选什么好?里面有个说学会Git是个很不错选择,今天就抽时间感受下Git的魅力吧.   Pro Git (Scott Chacon) 读书笔记:   ...

  7. 《Pro Android Graphics》读书笔记之第四节

    Android Procedural Animation: : XML, Concepts and Optimization Procedural Animation Concepts: Tweens ...

  8. 《Pro Android Graphics》读书笔记之第三节

    Android Frame Animation: XML, Concepts and Optimization Frame Animation Concepts: Cels, Framerate, a ...

  9. 《Pro Android Graphics》读书笔记之第六节

    Android UI Layouts: Graphics Design Using the ViewGroup Class Android ViewGroup Superclass: A Founda ...

随机推荐

  1. http1.0 和 http1.1 区别

    http1.0 和 http1.1 主要区别 1.背景   KeepAlive是就是通常所称的长连接.KeepAlive带来的好处是可以减少tcp连接的开销,这对于短response body的请求效 ...

  2. wireshark教程

    Wireshark世界上最流行的网络分析工具. 这个强大的工具能够捕捉网络中的数据,并为用户提供关于网络和上层协议的各种信息.与非常多其它网络工具一样.Wireshark也使用pcap network ...

  3. cocos2D(八)---- CCMenu &amp;&amp; CCMenuItem

    些菜单项让用户開始游戏.暂停\继续游戏.打开\关闭音乐或者是返回到上一个界面,比方以下两张图中用红色线框标记的菜单项     我们能够使用CCMenu和CCMenuItem实现上述的菜单功能,CCMe ...

  4. 在JAVA中使用LUA脚本记,javaj调用lua脚本的函数(转)

    最近在做一些奇怪的东西,需要Java应用能够接受用户提交的脚本并执行,网络部分我选择了NanoHTTPD提供基本的HTTP服务器支持,并在Java能承载的许多脚本语言中选择了很久,比如Rhino,Jy ...

  5. 使用GDAL图书馆RPC校正问题

    很快就会GDAL库更新1.11版本号之后,在发现之前写RPC像方误差修正模型校准结果特别大(在更新结果之前的版本号和PCI结果一致).所以初步推断是GDAL库的bug,经过各个參数改动发现原来是指定的 ...

  6. java.io.FileNotFoundException: /home/hadoop/hadoop/dfs/namenode/current/VERSION (Permission denied)

    今天布置hadoop集群,尝试单独将secondarynamenode分属到一台独立的虚拟机上, 当格式化后,start-dfs.sh.namenode没启动.查看日志.报错例如以下 查看权限才发现, ...

  7. Cocos2d-X在SwitchControl使用

    SwitchControl控制类中的一个开关的发挥了作用似在现实生活中开关 因为控制相对简单,我没有做过多的解释.直接在代码 首先在project文件夹下的Resource文件夹中加入三张图片 在Sw ...

  8. hdu 2899 hdu 3400 三分/几何

    hdu2899 : 水提,直接三分,事实上求导后二分也能够. #include<iostream> #include<cstdio> using namespace std; ...

  9. linux如何执行后台进程

    linux直接执行一个过程.电流指令结束后.或者关闭掉shell形成过程将结束. 如何在后台执行的处理 办法1 采用nohup命令,nohup命令本身的意思no hung up他说,他们将不会收到sh ...

  10. jquery 判断当前上传文件大小限制上传格式 搭配thinkphp实现上传即预览(模拟异步上传)

    在web开发中,最纠结的一项就是文件上传,最近由于项目需要前后摸索了四天在这里分享给大家.如有不足,望指出!! 前台:jquery.easyui.html 后台:thinkphp 主要涉及语言:jqu ...