Viewbag to edit row in grid.

Nov 10, 2011 at 9:24 AM

I created a grid. The grid contains rows that have a dropdown box in them. At first I created a static dropdownbox. I would like to fill the values from the database. The values are retrieved from the database and stored in the ViewBag. Is there anyway to make them available in the edit template?


Nov 10, 2011 at 9:53 AM

I found what I did wrong. The template is generated serverside. So I dont need to use the viewbag but I can call them method that provides the keyvalue pairs for me.....


Nov 10, 2011 at 11:22 AM

As a general rule when you are in a template the VieewBag of the template doesn't contain a copy of the data of the main View. Reason: 1) performance: copyng them in several items might have a negative impact on performance 2) modularity...not good for modularity 



1) I don't like viewbag...but I like just ViewModels.... Add a pointer to the data you need in the item data class. That is create a ViewModel that encloses the Business data of each item plus the further data you need. Then when you receive data from the business layer do a simple foreach and build a list of Item ViiewModels that will substritute the initial list of data items. THIS IS THE SOULUTION I ALWAYS ADOPT

2) define the item template in the same view as the greed with a RazorHelper, so you can access the ViewBag of the MainView from there. In thiscase you will have basically two helpers: the one passed as an argument to the Razor helper that you can use to render TextBoxes, and other controls, and the original View Helper that you can still access throught the Html property of the can use this last to access the MainWindow ViewBag....Dosn't do this too much otherwise you will loose a lot in terms of modularity.