asp.net mvc 之 asp.net mvc 3.0 新特性之 View(Razor):
- Razor 的语法
- Razor 与 Model
- Razor 与布局
示例 1、Razor 概述 RazorDemoController.cs
using System.Collections.Generic; |
namespace MVC30.Controllers |
public class RazorDemoController : Controller |
public ActionResult Summary() |
Summary.cshtml
ViewBag.Title = "Razor 概述"; |
使用 Razor 之前,要在 Web.Config 中做如下配置 |
< textarea rows = "20" style = "width: 100%" > |
< sectionGroup name = "system.web.webPages.razor" type = "System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup,
System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" > |
< section name = "host" type = "System.Web.WebPages.Razor.Configuration.HostSection,
System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" requirePermission = "false" /> |
< section name = "pages" type = "System.Web.WebPages.Razor.Configuration.RazorPagesSection,
System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35" requirePermission = "false" /> |
< system.web.webPages.razor > |
< host factoryType = "System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" /> |
< pages pageBaseType = "System.Web.Mvc.WebViewPage" > |
< add namespace = "System.Web.Mvc" /> |
< add namespace = "System.Web.Mvc.Ajax" /> |
< add namespace = "System.Web.Mvc.Html" /> |
< add namespace = "System.Web.Routing" /> |
</ system.web.webPages.razor > |
View 在每次 Render 之前都会先执行 _ViewStart.cshtml 中的代码 |
2、Razor 语法简介 RazorDemoController.cs
using System.Collections.Generic; |
namespace MVC30.Controllers |
public class RazorDemoController : Controller |
public ActionResult Syntax() |
Syntax.cshtml
ViewBag.Title = "Razor 语法"; |
使用@符号加{},直接在 html 页面中写 C# |
@{ var currentTime = DateTime.Now; } @* 相当于 <% Htmlvar currentTime = DateTime.Now; %> *@ |
@currentTime.ToString("yyyy-MM-dd") |
使用@符号,直接在 html 页面中写 C# 并输出结果 |
@Request.Url @* 相当于 <%= Request.Url %> *@ |
例:AsInt(), IsInt(), AsBool(), IsBool(), AsFloat(), IsFloat(), AsDecimal(), IsDecimal(), AsDateTime(), IsDateTime() |
类似 AsInt() 的方法会有一个重载方法 AsInt(int defaultValue),用于当转换失败时返回指定的默认值 |
var date = tmp.AsDateTime(); |
@date.ToString("yyyy-MM-dd") |
获取文件的 URL 绝对路径的方法,一般用于 img 标签,link 标签,a 标签中所引用的文件的完全 url 路径 |
< img alt = "" src = "@Href(" ~/Content/themes/base/images/ui-icons_888888_256x240.png")" /> |
Html Helper, Ajax Helper, Url Helper 依然可以使用 |
@Html.TextBox("txt", "我是 TextBox") |
3、Razor 的与 Model 相关的 Demo User.cs
using System.Collections.Generic; |
public int ID { get ; set ; } |
public string Name { get ; set ; } |
public string Password { get ; set ; } |
public string ConfirmPassword { get ; set ; } |
public DateTime DateOfBirth { get ; set ; } |
public string Comment { get ; set ; } |
RazorDemoController.cs
using System.Collections.Generic; |
namespace MVC30.Controllers |
public class RazorDemoController : Controller |
public ActionResult Model() |
ViewBag.Author = "webabcd" ; |
var list = new List<User>() |
new User { ID = 1, Name = "webabcd" , DateOfBirth = new DateTime(1980, 2, 14), Comment = "<b>mvp</b>" }, |
new User { ID = 2, Name = "prettygyy" , DateOfBirth = new DateTime(1981, 6, 26), Comment = "<b>mvp</b>" } |
_MyLayout_ParitalView.cshtml
通过 @model 指定 Model 的类型,同时 Model 对象就是 Html.Partial() 或 Html.RenderPartial() 时传递过来的对象 |
Model.cshtml
通过 @model 指定 Model 的类型,同时 Model 对象就是 Action 返回的数据 |
ViewBag.Title = "Razor 的与 Model 相关的 Demo"; |
@foreach (var user in Model) |
if (@user.Name == "webabcd") |
< li >@user.Name (@Html.Raw(@user.Comment))</ li > |
< li >@user.Name (@user.Comment)</ li > |
@foreach (var user in Model) |
@Html.Partial("_MyLayout_ParitalView", user) |
<%= Html.Partial("_MyLayout_ParitalView", user) %> |
var firstUser = Model.First(); |
Html.RenderPartial("_MyLayout_ParitalView", firstUser); |
<% Html.RenderPartial("_MyLayout_ParitalView", firstUser); %> |
|
请发表评论