Sunday, December 30, 2018

Viewbag in MVC

Viewbag is used to shares the small amount of data from the controller to view. the Viewbag is a Dynamic type which means no need to typecasting and downcasting. the lifespan of Viewbag is last current HTTP request.

Example :

 namespace Alltechgeeks.Controllers
  public class TestController  : Controller
       IList <Test> testList  = new List<Test>()
                                               new Test(){id=1, name="Murali"},
                                               new Test(){id=2, name="Sathwiq"},
                                               new Test(){id=3, name="Siri"},
                                               new Test(){id=4, name="Krishna"}
           public ActionResult Index()
                  Viewbag.totaltest   = testList.Count();
                   return View();



Go to Index.cshtml page consume the viewbag.totaltest in view

        Total Test : @Viewbag.totaltest

we can assign any number of values and properties to Viewbag.

No comments: