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.

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 Views\Home\IndexNotStonglyTyped.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>

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.

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:

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.

Dynamic V Strongly Typed Views的更多相关文章

  1. MVC 5 Strongly Typed Views(强类型视图)

    学习MVC这样久以来,发觉网站上很多MVC的视频或是文章,均是使用Strongly Type views来实现控制器与视图的交互.Insus.NET以前发布的博文中,也大量使用这种方式: <Da ...

  2. a loosely strongly typed language

    JavaScript: The Definitive Guide, Sixth Edition by David Flanagan As explained above, the following ...

  3. ASP.NET Core 中文文档 第二章 指南(4.3)添加 View

    原文:Adding a view 作者:Rick Anderson 翻译:魏美娟(初见) 校对:赵亮(悲梦).高嵩(Jack).娄宇(Lyrics).许登洋(Seay).姚阿勇(Dr.Yao) 本节将 ...

  4. ASP.NET MVC 5 - 将数据从控制器传递给视图

    在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图.控制器类将响应请求来的URL.控制器类是给您写代码来处理传入请求的地方,并从数据库中检索数据,并最终决定什么类型的返回结果 ...

  5. MVC 5 - 将数据从控制器传递给视图

    MVC 5 - 将数据从控制器传递给视图 在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图.控制器类将响应请求来的URL.控制器类是给您写代码来处理传入请求的地方,并从数据 ...

  6. [转]ASP.NET MVC 5 - 将数据从控制器传递给视图

    在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图.控制器类将响应请求来的URL.控制器类是给您写代码来处理传入请求的地方,并从数据库中检索数据,并最终决定什么类型的返回结果 ...

  7. [引]ASP.NET MVC 4 Content Map

    本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...

  8. [.NET MVC进阶系列03] Views 视图基础

    [注:此文对应Chapter 3:Views] 一.View的功能: 1.View用来呈现页面UI,通过Controller来指定View: 要注意的是,MVC和以前基于文件的Web应用不同,URL指 ...

  9. [转]Load ASP.NET MVC Partial Views Dynamically Using jQuery

    本文转自:http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx Most of the t ...

随机推荐

  1. 初涉hash

    今天和朋友讨论一个问题 有两百亿个数,我给出一个数,找到这两百亿个数中两数相加等于它的组合.要求时间复杂度为线性,空间2G 解决思路是开一个hash表比如a[1000]将所有数存入hash表中,a[i ...

  2. 帝国时代II 高清版 steam 4.4 字体替换 微软雅黑

    其实默认的中文字体算是中规中矩吧,但是我并不喜欢 从昨天开始就想着换 于是我就开始搜索帝国时代2的游戏目录的资源,马上就锁定到了\Steam\steamapps\common\Age2HD\resou ...

  3. Windows环境下npm install常见错误

    Windows环境下npm install安装包依赖时,常出现一些错误,下面为个人解决办法: 错误一 缺少python环境: G:\nodejs\moviesite\node_modules\bcry ...

  4. Controller

    http://www.cnblogs.com/artech/archive/2012/08/15/action-result-03.html

  5. css 超过宽度显示...

    一般使用 display:block;white-space:nowrap; overflow:hidden; text-overflow:ellipsis; *****************未实验 ...

  6. php自学提升进阶路线

    为了自己对php的系统全面深入的掌握,我通过个人经验,以及搜索网上高手经验,汇总了一份php自我学习路线规划,包括实战演练.学习建议.高手进阶.常见问题和测试总结五块.算是一个系统的学习计划和目标吧. ...

  7. [转]''\\=DevExpress 中 汉化包 汉化方法

    [DevExpress]DevExpress 中 汉化包 汉化方法 2016-02-17 21:13 1781人阅读 评论(0) 收藏 举报  分类: DevExpress(24)  版权声明:本文为 ...

  8. MyBatis入门学习教程-MyBatis快速入门

    一.Mybatis介绍 MyBatis是一个支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及对结果集的检索封装.MyBatis可以 ...

  9. ElasticSearch优化配置

    ################################################################### /etc/elasticsearch/elasticsearch ...

  10. 浅谈人脸检测之Haar分类器方法

    我们要探讨的Haar分类器实际上是Boosting算法(提升算法)的一个应用,Haar分类器用到了Boosting算法中的AdaBoost算法,只是把AdaBoost算法训练出的强分类器进行了级联,并 ...