首页 > 代码库 > Asp.Net MVC向视图View传值的三种常见的方法:
Asp.Net MVC向视图View传值的三种常见的方法:
1.通过View( parameter )的参数传递
action:
public ActionResult Index()
{
Person person=new Person("wumiao",18,175);
return View("Index",person);
}
view:
@using test_01 //添加自定义类所在命名空间的引用
@{ var person=(Person)ViewData.Model; }
姓名:@person.Name - 年龄:@person.Age - 身高: @person.Height
---------------------------------------------------------------------------
2.通过ViewBag.key传递
action:
public ActionResult Index()
{
Person person=new Person("wumiao",18,175);
ViewBag.Person=person;
return view();
}
view:
@using test_01
@{ var person = ViewData.Person; }
姓名:@person.Name - 年龄:@person.Age - 身高: @person.Height
------------------------------------------------------------------------
3.通过ViewData[key]传递
action:
public ActionResult Index()
{
Person person=new Person("wumiao",18,175);
ViewData["person"]=person;
return View("Index");
}
view:
@using test_01
@{ var person=(Person)ViewData["person"]; }
姓名:@person.Name - 年龄:@person.Age - 身高: @person.Height
Asp.Net MVC向视图View传值的三种常见的方法: