《精通MVC5.0》笔记Razor

1.1.视图声明数据类型

Razor声明都是@开始,例如@model MVC.Models.Product声明了控制器创给视图的数据类型,这样就可以在视图使用@Modle.property访问数据,如下@Model.Name,注意此处Model首字母大写,声明数据类型model首字母小写

@model MVC.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>
对应的控制器方法:
 public class HomeController : Controller
    {
        public Product MyProduct = new Product()
        {
            ProductID = 1,
            Name = "Kayak",
            Description = "A boat for one person",
            Category = "Watersports",
            Price = 275M
        };
        // GET: Home
        public ActionResult Index()
        {
            return View(MyProduct);
        }
    }

1.2.视图模板设置

模板文件命名约定_开头,例如_BasicLayout.cshtml

@RenderBody

@model MVC.Models.Product

@{
    ViewBag.Title = "Product Name";
    Layout = "~/Views/_BasicLayout.cshtml";
}

@Model.Name

1.3.使用ViewStart文件

如果每个视图页面都使用到了同一个模板文件,那么是否每个视图页面都要设置一次Layout = "....."岂不是很麻烦。使用_ViewStart.cshtml可以指定默认模板可以解决,如下_ViewStart.cshtml

@{
    Layout = "~/Views/_BasicLayout.cshtml";
}

此时Index.cshtml页面就不用指定模板,默认模板使用_BasicLayout.cshtml

@model MVC.Models.Product

@{
    ViewBag.Title = "Product Name";
}

@Model.Name

1.4.使用Razor Expressions

控制器方法应该做的是传一个数据对象或对象集合给对应视图,而不是确切的数据。记住传给视图的数据是对应数据类型一个对象或对象集合。假如有个视图页面Price.cshtml用于展示价格,此时对应的控制器方法应该是return View(myProduct),而不是return View(myProduct.Price),然后在视图页面再用Razor @Model 表达式获取确切数据,例如

 

The Product name is @Model.Name,and it costs $@Model.Price

 

1.5.使用ViewBag传数据给视图

//控制器方法

        public ActionResult DemoViewBag()
        {
            ViewBag.ProductCount = 1;
            ViewBag.ExpressShip = true;
            ViewBag.ApplyDiscount = false;
            ViewBag.Supplier = null;
            return View(MyProduct);
        }

//对应视图
@model MVC.Models.Product

@{
    ViewBag.Title = "DemoViewBag";
}

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

1.6.控制器传的数据作为属性值

 

//控制器方法
public ActionResult DemoViewBag()
        {
            ViewBag.ProductCount = 1;
            ViewBag.ExpressShip = true;
            ViewBag.ApplyDiscount = false;
            ViewBag.Supplier = null;
            return View(MyProduct);
        }
//对应视图
@model MVC.Models.Product

@{
    ViewBag.Title = "DemoViewBag";
}

<div data-discount="@ViewBag.ApplyDiscount" express="@ViewBag.ExpressShip" data-supplier="@ViewBag.Supplier">
    The containing element has data attributes
</div>
Discount1:<input type="checkbox" checked="@ViewBag.ApplyDiscount"/>
Discount2:<input type="checkbox" checked="@Model.isDiscount"/>
Express:<input type="checkbox" checked="@ViewBag.ExpressShip"/>

1.7.Razor条件表达式

条件表达式以@开始,}结束

在视图中插入纯文本,且没有被html标签包围,需要@:前缀,例如@:Out of Stock

 

@model MVC.Models.Product

@{
    ViewBag.Title = "DemoViewBag";
}
<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>
@model MVC.Models.Product

@{
    ViewBag.Title = "DemoViewBag";
}
<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 Stoc
                }
                else if (ViewBag.ProductCount == 1)
                {
                    <b>Low Stock(@ViewBag.ProductCount)</b>
                }else
                {
                    @ViewBag.ProductCount
                        
                }
            </td>
        </tr>
    </tbody>
</table>

 1.8.Razor遍历数组和集合

注意:在视图页面引入命名空间与后台代码一样:@using 命名空间

//控制器方法  
public ActionResult DemArray()
        {
            Product[] array = {
                new Product() { Name = "kaya",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);
        }
//对应视图
@model MVC.Models.Product[]
@using MVC.Models
@{
    ViewBag.Title = "DemArray";
    Layout = "~/Views/_BasicLayout.cshtml";
}

<h2>DemArray</h2>
@if (Model.Length > 0)
{
    <table>
        <thead><tr><th>Product</th><th>Price</th></tr></thead>
        <tbody>
            @foreach (Product p in Model)
            {
                <tr>
                    <td>@p.Name</td>
                    <td>$@p.Price</td>
                </tr>
            }
        </tbody>
    </table>
}

 

原文地址:https://www.cnblogs.com/come-on-come-on/p/5634371.html