0

Like in topic. It is possible to do it? Or make something to use ViewBag items in place where we need no dynamic item?

Mateusz Rogulski
  • 7,357
  • 7
  • 44
  • 62

3 Answers3

4

The best way is to create View model and use that model in your View rather then ViewBag.

Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
2

It is probably not preferable, since you probably should use a model in that case, but in the view, you can always cast the viewbag property into a type, if you need it to be an int for instance,

int number = (int)ViewBag.MyNumber;
Christofer Eliasson
  • 32,939
  • 7
  • 74
  • 103
1

If you can't use a dynamic item, just declare a variable with the ViewBag's "static" type :

@IList<MyType> mylist= ViewBag.ListOfItemsOfMyType;

and use mylist

Raphaël Althaus
  • 59,727
  • 6
  • 96
  • 122