1、ViewBag
动态型,动态视图字典,可以直接使用
//controller中 ViewBag.num = ID; ViewBag.name = name; //View中 @ViewBag.num; @ViewBag.name;
2、ViewData
弱类型,视图数据的字典,使用时必须转化为类型或数组
//controller中 ViewData["Gender"] = "女"; ViewData["Age"] = 18; //View中 @ViewData["Gender"]; @ViewData["Age"];
3、TempData
临时存储,临时数据的字典
ViewBag和ViewData的声明周期只有当前页面的控制器到页面,一旦发生redirection就不存在,但TempData可以,因此可用于页面跨页面传值
//controller中 TempData["Welcome"] = "Hello, Welcome to the page."; //View中 @TempData["Welcome"]
4、model
model传递的是对象,也可以传递一个集合
//model中
public class User
{
string _name;//字段
public string Name //属性
{
get { return _name; }
set { _name = value; }
}
public User(string name)
{
this.Name = name;
}
}
//controller中
public ActionResult Index()
{
User user = new User("Erin");
ViewData.Model = user;
return View();
}
//View中
@ViewData.Model.Name;
二、实现页面跳转的方式
View中
product https://blog.csdn.net/linxianming_/article/details/message @*链接到控制器下的https://blog.csdn.net/linxianming_/article/details/message方法*@ @Html.ActionLink("product https://blog.csdn.net/linxianming_/article/details/message", "https://blog.csdn.net/linxianming_/article/details/message") @*使用HTML辅助器生成元素*@
加载部分视图的方法
@Html.Partial("_https://blog.csdn.net/linxianming_/article/details/message")
@{
Html.RenderPartial("_https://blog.csdn.net/linxianming_/article/details/message");
}



