zoukankan      html  css  js  c++  java
  • MVC2.0==>MVC3.0

    总结出如下4个MVC3.0和2.0的重要区别。

    1. @ 符号在 View 页面中的用法:

    C#代码以 @符号开头,例如

    1 <h2>Name: @Model.Name</h2>2 3 以及4  5 @if (Model.Category == "Watersports") { 6     <p>@Model.Category <b>Splash!</b> </p> 7 }

    如果你的代码中不是以 html 标记开头,也就是说你想在一段文本内部使用 C#代码的话,使用 @: 标记,例如

    @:Category: @Model.Category <b>Splash!</b>

    你可以通过 @{ } 把一大块代码组织起来,例如

    @{    if (Model.Category == "Watersports") {        @:Category: @Model.Category <b>Splash!</b>     }    if (Model.Price > 10) {        <h5>Pricey!</h5>    }}

    假如你使用的是 html 标记开头的话,可以写成这样

    <span>Category: @Model.Category <b>Splash!</b></span>

     建立强类型的 view 页面之后,页头中使用 @model (必须为小写字母)指明对象的类型,例如 @model Razor.Models.Product

    引用命名空间在页头中使用 @using 例如 @using System.Data;

    2. Controler 向 View 传值

    以前的方法仍然可用,比如 return view(model) ,用于向强类型的页面传入对象,或者使用 ViewData

    MVC3 又增加了 ViewBag ,这是一个动态类型,意思是说你可以不需要定义它的属性和数据类型,在使用的时候直接使用。

    例如 ViewBag 根本没有 aaa 这个属性,也不知道 aaa 到底是什么类型,你可以直接 ViewBag.aaa=123 这样用,

    系统自动给 ViewBag 增加一个 int 型的属性 aaa 并赋值 123 ,在 View 页面中可以直接调用 ViewBag.aaa 获取到 123

    3.母板页的使用

    在创建 view 的时候,如果勾选了“使用母板页”的复选框,但并没有指定母板页的名字,

    系统默认使用的是 _ViewStart.cshtml 页面中通过 Layout 指定的母板页。

    如果新创建的 View 中不需要母板页,则必须有以下代码:

    @{    Layout = null;}

     
    4. 呈现部分视图时使用 @Html.Action、@Html.Partial和@Html.RenderPartial 方法

       语法如下:

    @Html.Action("PartialViewName")@Html.Partial("PartialViewName")@{ Html.RenderPartial("PartialViewName"); }
    转载 请注明原文地址并标明转载:http://www.cnblogs.com/laopo 商业用途请与我联系:lcfhn168@163.com
  • 相关阅读:
    ubuntu安装 scala
    提交jar作业到spark上运行
    在IDEA上用python来连接集群上的hive
    spark在eclipse上配置
    【Spring AOP】Spring AOP的使用方式【Q】
    【Spring 源码】ApplicationContext源码
    【Spring 源码】Spring 加载资源并装配对象的过程(XmlBeanDefinitionReader)
    【Spring Cloud】Spring Cloud使用总结
    【坑】不要使用各种框架提供的内部List
    Lombok的使用
  • 原文地址:https://www.cnblogs.com/laopo/p/4557631.html
Copyright © 2011-2022 走看看