This article summarizes the three common ways that ASP. NET MVC will pass values to view views:
--------------------------------------------------------------------------
1. Pass through the parameters of view (parameter)
Action
Public ActionResult Index ()
{
Person Person=new person ("Wumiao", 18,175);
Return View ("Index", person);
}
View
@using test_01//Add a reference to the namespace where the custom class resides
@{var person= (person) Viewdata.model; }
Name: @person. Name-Age: @person. Age-Height: @person. Height
---------------------------------------------------------------------------
2. Transfer via Viewbag.key
Action
Public ActionResult Index ()
{
Person Person=new person ("Wumiao", 18,175);
Viewbag.person=person;
return view ();
}
View
@using test_01
@{var person= (person) viewdata.per;}
Name: @person. Name-Age: @person. Age-Height: @person. Height
------------------------------------------------------------------------
3. Pass through Viewdata[key]
Action
Public ActionResult Index ()
{
Person Person=new person ("Wumiao", 18,175);
viewdata["Person"]=person;
}
View
@using test_01
@{var person (person) viewdata[' person ';}
Name: @person. Name-Age: @person. Age-Height: @person. Height
----------------------------------------------------------------------------
Three ways that ASP. NET MVC spreads values to view views