MVC 控制器向View传值的三种方法
2015-09-24 13:41
549 查看
分类: MVC2013-06-07
21:27 17302人阅读 评论(5) 收藏 举报
MVC
View 传值
1.提供视图模型对象
你能把一个对象作为View方法的参数传递给视图.
[csharp] view plaincopyprint?public ViewResult Index()
{
DateTime date = DateTime.Now;
return View(date);
}
然后我们在视图中使用Razor的Model关键字来访问这个对象
[csharp] view
plaincopyprint?
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
The day is: @(((DateTime)Model).DayOfWeek)
或者是
[csharp] view
plaincopyprint?
@model DateTime
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
The day is: @Model.DayOfWeek
2.使用ViewBag(视图包)传递数据
View Bag 允许在一个动态的对象上定义任意属性,并在视图中访问它.这个动态的对象可以通过Controller.ViewBag属性访问它.
[csharp] view plaincopyprint?public ViewResult Index()
{
ViewBag.Message = "Hello";
ViewBag.Date = DateTime.Now;
return View();
}
@{
ViewBag.Title = "Index";
}
<h>Index</h>
The day is: @ViewBag.Date.DayOfWeek
<p />
The message is: @ViewBag.Message
3. 使用View Data传递数据
在MVC3.0之前,主要是通过这种方式传递数据,它是通过用 ViewDataDictionary类实现的,而不是动态的对象.ViewDataDictionary类是类似标准"键/值"集合,并通过
Controller类的ViewData属性进行访问的.这个方法,在视图中需要对对象进行转换.
[csharp] view
plaincopyprint?
控制器中:
public ViewResult Index()
{
ViewData["Message"] = "Hello";
ViewData["Date"] = DateTime.Now;
return View();
}
视图中:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
The day is: @(((DateTime)ViewData["Date"]).DayOfWeek)
<p />
The message is: @ViewData["Message"]
21:27 17302人阅读 评论(5) 收藏 举报
MVC
View 传值
1.提供视图模型对象
你能把一个对象作为View方法的参数传递给视图.
[csharp] view plaincopyprint?public ViewResult Index()
{
DateTime date = DateTime.Now;
return View(date);
}
然后我们在视图中使用Razor的Model关键字来访问这个对象
[csharp] view
plaincopyprint?
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
The day is: @(((DateTime)Model).DayOfWeek)
或者是
[csharp] view
plaincopyprint?
@model DateTime
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
The day is: @Model.DayOfWeek
2.使用ViewBag(视图包)传递数据
View Bag 允许在一个动态的对象上定义任意属性,并在视图中访问它.这个动态的对象可以通过Controller.ViewBag属性访问它.
[csharp] view plaincopyprint?public ViewResult Index()
{
ViewBag.Message = "Hello";
ViewBag.Date = DateTime.Now;
return View();
}
@{
ViewBag.Title = "Index";
}
<h>Index</h>
The day is: @ViewBag.Date.DayOfWeek
<p />
The message is: @ViewBag.Message
3. 使用View Data传递数据
在MVC3.0之前,主要是通过这种方式传递数据,它是通过用 ViewDataDictionary类实现的,而不是动态的对象.ViewDataDictionary类是类似标准"键/值"集合,并通过
Controller类的ViewData属性进行访问的.这个方法,在视图中需要对对象进行转换.
[csharp] view
plaincopyprint?
控制器中:
public ViewResult Index()
{
ViewData["Message"] = "Hello";
ViewData["Date"] = DateTime.Now;
return View();
}
视图中:
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
The day is: @(((DateTime)ViewData["Date"]).DayOfWeek)
<p />
The message is: @ViewData["Message"]
相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- Zend的MVC机制使用分析(二)
- ASP.NET MVC 4 捆绑和缩小实例介绍
- ASP.NET MVC中将控制器分离到类库的实现
- asp.net实现在非MVC中使用Razor模板引擎的方法
- ASP.NET MVC中的AJAX应用
- 如何在MVC应用程序中使用Jquery
- ASP.NET MVC小结之基础篇(二)
- ASP.NET小结之MVC, MVP, MVVM比较以及区别(一)
- Asp.net实现MVC处理文件的上传下载功能实例教程
- ASP.NET MVC小结之基础篇(一)
- 浅析Asp.net MVC 中Ajax的使用
- JSP使用MVC模式完成删除和修改功能实例详解
- Flex中最好的MVC框架Mate框架
- ASP.NET MVC学习笔记
- 关于php mvc开发模式的感想
- php打造属于自己的MVC框架
- 使用ASP.NET MVC 4 Async Action+jQuery实现消息通知机制的实现代码
- mvc下,3种窗口弹出设置的方法
- ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法