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:
- As a strongly typed model object.
- As a dynamic type (using @model dynamic)
- 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:
- Automatic scaffolding
- Intellisense
- Compile time type checking (follow link to enable this)
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:
- ScottGu’s post on ViewBag
- ViewBag dynamic in ASP.NET MVC 3 – by Hanan
- ViewBag in ASP.NET MVC 3 by David Hayden
The c# project can be downloaded here.
Dynamic V Strongly Typed Views的更多相关文章
- MVC 5 Strongly Typed Views(强类型视图)
学习MVC这样久以来,发觉网站上很多MVC的视频或是文章,均是使用Strongly Type views来实现控制器与视图的交互.Insus.NET以前发布的博文中,也大量使用这种方式: <Da ...
- a loosely strongly typed language
JavaScript: The Definitive Guide, Sixth Edition by David Flanagan As explained above, the following ...
- ASP.NET Core 中文文档 第二章 指南(4.3)添加 View
原文:Adding a view 作者:Rick Anderson 翻译:魏美娟(初见) 校对:赵亮(悲梦).高嵩(Jack).娄宇(Lyrics).许登洋(Seay).姚阿勇(Dr.Yao) 本节将 ...
- ASP.NET MVC 5 - 将数据从控制器传递给视图
在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图.控制器类将响应请求来的URL.控制器类是给您写代码来处理传入请求的地方,并从数据库中检索数据,并最终决定什么类型的返回结果 ...
- MVC 5 - 将数据从控制器传递给视图
MVC 5 - 将数据从控制器传递给视图 在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图.控制器类将响应请求来的URL.控制器类是给您写代码来处理传入请求的地方,并从数据 ...
- [转]ASP.NET MVC 5 - 将数据从控制器传递给视图
在我们讨论数据库和数据模型之前,让我们先讨论一下如何将数据从控制器传递给视图.控制器类将响应请求来的URL.控制器类是给您写代码来处理传入请求的地方,并从数据库中检索数据,并最终决定什么类型的返回结果 ...
- [引]ASP.NET MVC 4 Content Map
本文转自:http://msdn.microsoft.com/en-us/library/gg416514(v=vs.108).aspx The Model-View-Controller (MVC) ...
- [.NET MVC进阶系列03] Views 视图基础
[注:此文对应Chapter 3:Views] 一.View的功能: 1.View用来呈现页面UI,通过Controller来指定View: 要注意的是,MVC和以前基于文件的Web应用不同,URL指 ...
- [转]Load ASP.NET MVC Partial Views Dynamically Using jQuery
本文转自:http://www.binaryintellect.net/articles/218ca630-ba50-48fe-af6e-6f754b5894aa.aspx Most of the t ...
随机推荐
- Django 创建APP简单步骤
yum install epel-releaseyum install python34yum install python-pippip install django django-admin st ...
- 《C与指针》第十一章练习
本章问题 1.在你的系统中,你能够声明的静态数组最大的长度能达到多少?使用动态内存分配,你最大能获取的内存块有多少? answer: This will vary from system to sys ...
- lua-nginx-module 学习
下载安装LuaJIT cd /usr/local/src sudo wget http://luajit.org/download/LuaJIT-2.0.3.tar.gz tar -xzvf LuaJ ...
- Win32隐藏输出console窗口
#include <Windows.h> void HideConsole() { ::ShowWindow(::GetConsoleWindow(), SW_HIDE); } void ...
- 原生select默认显示为空胡fish覅神农大丰今年圣诞节奋笔疾书发撒可交付你说的尽快发那段时间南方大厦尽可能放你的所发生的你富家大室耐腐蚀的看法呢尽快发你上课积啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊撒啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊分你束带结发你看
下拉框默认为空: <select> <option value="" class="blank"></option> < ...
- Java相关
1.多线程实现方法? 1).继承Thread类实现多线程 2).实现Runnable接口方式实现多线程 3).使用ExecutorService.Callable.Future实现有返回结果的多线程 ...
- 关于python如何简单跳出多层循环
上述代码的逻辑是,在跳出子循环之前定义一个变量flag为Ture,第一层循环跳出之后,如果要跳出第二个循环,直接调用变量flag,可以直接跳出第二层循环.这里要注意的是缩进,不然会出错误. 如果是多层 ...
- winfrom自定义控件
c#TabControl控件左边选项卡左边显示,文字横向显示 http://blog.csdn.net/kasama1953/article/details/52133713 WinForm中,给Te ...
- DDoS
Distributed Denial of Service (DDoS) Attacks/tools https://staff.washington.edu/dittrich/misc/ddos/ ...
- 【linux】 解决linux下vsftp 500 OOPS: cannot change directory:/home/ftp/ 办法
用FileZilla连接ftp出现错误,500 OOPS: cannot change directory:/home/ftp 原因是CentOS系统安装了SELinux,因为默认下是没有开启FTP的 ...