Asp. Net MVC three methods for passing values to a View: asp. netmvc
This article summarizes three common methods for passing values from Asp. Net MVC to a View:
--------------------------------------------------------------------------
1. pass through View (parameter) Parameters
Action:
Public ActionResult Index ()
{
Person person = new Person ("wumiao", 18,175 );
Return View ("Index", person );
}
View:
@ Using test_01 // Add reference to the namespace where the custom class is located
@ {Var person = (Person) ViewData. Model ;}
Name: @ person. Name-Age: @ person. Age-Height: @ person. Height
---------------------------------------------------------------------------
2. pass through 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
----------------------------------------------------------------------------