zoukankan      html  css  js  c++  java
  • Dynamic V Strongly Typed Views

    Come From https://blogs.msdn.microsoft.com/rickandy/2011/01/28/dynamic-v-strongly-typed-views/

    There are three ways to pass information from a controller to a view in ASP.NET MVC 3:

    1. As a strongly typed model object.
    2. As a dynamic type (using @model dynamic)
    3. Using the ViewBag

    I’ve written a simple MVC 3  Top Blog application to compare and contrast dynamic and strongly typed views. The controller starts out with a simple list of blogs:

    using System.Collections.Generic;
    using System.Web.Mvc;
    
    namespace Mvc3ViewDemo.Controllers {
    
        public class Blog {
            public string Name;
            public string URL;
        }
    
        public class HomeController : Controller {
    
            List<Blog> topBlogs = new List<Blog>
          { 
    new Blog { Name = "ScottGu", URL = "http://weblogs.asp.net/scottgu/"},
    new Blog { Name = "Jon Galloway", URL = "http://weblogs.asp.net/jgalloway"},
    new Blog { Name = "Scott Hanselman", URL = "http://www.hanselman.com/blog/"}
          };
    
            public ActionResult IndexNotStonglyTyped() {
                return View(topBlogs);
            }
    
            public ActionResult StonglyTypedIndex() {
                return View(topBlogs);
            }
    
            public ActionResult IndexViewBag() {
                ViewBag.BestBlogs = topBlogs;
                return View();
            }
        }
    }

    Right-click in the IndexNotStonglyTyped() method and add a Razor view.

    alt

    Make sure the Create a strongly-typed view box is not checked. The resulting view doesn’t contain much:

    @{
        ViewBag.Title = "IndexNotStonglyTyped";
    }
    
    <h2>IndexNotStonglyTyped</h2>
    

    On the first line of the ViewsHomeIndexNotStonglyTyped.cshtml file, add the model directive and the dynamic keyword.

    @model dynamic
    

    Because we’re using a dynamic and not a strongly typed view, intellisense doesn’t help us. The completed code is shown below:

    @model dynamic
               
    @{
        ViewBag.Title = "IndexNotStonglyTyped";
    }
    
    <h2>Index Not Stongly Typed</h2>
    
    <p>
     <ul>
    @foreach (var blog in Model) {
       <li>
        <a href="@blog.URL">@blog.Name</a>
       </li>   
    }
     </ul>
    </p>
    

    alt

    Now we’ll add a strongly typed view. Add the following code to the controller:

    public ActionResult StonglyTypedIndex() {
        return View(topBlogs);
    }

    Notice it’s exactly the same return View(topBlogs); call as the non-strongly typed view. Right click inside of StonglyTypedIndex() and select Add View. This time select the Blog Model class and select List as the Scaffold template.

    alt

    Inside the new view template we get intellisense support and our view model is automatically scaffolded. Those are significant advantages and why ASP.NET MVC applications typically use strongly typed views. Strongly-typed view gives you:

    alt

    Another non-strongly typed way we can pass the top blogs into a view template is to use the view bag.  ViewBag is new to MVC 3 and has the advantage that it can be used in combination with a strongly typed model, giving you the advantages for both. ViewBag is useful when you want to pass information not related to the view model and you don’t want to create a view model just to pass the information. For example, you can use it to pass information to your layout template. Be sure to read ScottGu’s post where he talks about ViewBag.

    Add the following action method to the controller:

    public ActionResult IndexViewBag() {
                ViewBag.BestBlogs = topBlogs;
                return View();
            }

    The IndexViewBag.cshtml view template :

    @{
        ViewBag.Title = "Index_ViewBag";
    }
    
    <h2>Index View Bag</h2>
    
    <p>
     <ul>
    @foreach (var blog in ViewBag.BestBlogs) {
       <li>
        <a href="@blog.URL">@blog.Name</a>
       </li>   
    }
     </ul>
    </p>
    

    Good ViewBag links:

    The c# project can be downloaded here.

  • 相关阅读:
    Windows Server 2003下ASP.NET无法识别IE11的解决方法
    SQL Server2005中使用XML-数据类型、查询与修改
    连接SQLServer时提示“但是在登录前的握手期间发生错误。 (provider: SSL Provider, error: 0
    无法将类型为 excel.applicationclass 的 com 强制转换为接口类型 的解决方法。
    C# WinForm使用Aspose.Cells.dll 导出导入Excel/Doc 完整实例教程
    技巧 获取电脑硬件信息 -转发
    浏览器无需下载插件 解决网页长截图的小技巧 -转发
    note 9 列表、时间复杂度、排序
    note 8 字符串
    note 7 递归函数
  • 原文地址:https://www.cnblogs.com/cpcpc/p/MVC.html
Copyright © 2011-2022 走看看