ViewData in Asp.Net Mvc application
- ViewData is one of the property of ControllerBase class.
- It is described as dictionary object whichis derived from ViewDataDictionary class.Thse syntax of it is as follows:-
public ViewDataDictionary ViewData { get; set; }
- Passing and maintaing data from controller to corresponding view is done using ViewData.
- Their only aim is to provide communication between controller and corresponding view therefore ViewData life is short i.e within current request and it's value becomes null whenever redirection occur.
- Type casting for complex data type ViewData value is required.
Example of ViewData:
Controller with Index action have ViewData:-
public ActionResult Index()
{
ViewData["Name"] = "Suraj rana";
return View();
}
ViewData is used in a view. With respect to Index action a view is called and data between them is maintained by ViewData:-
@ViewData["Name"]
0 Comment(s)