Mateusz Rogulski
Mateusz Rogulski

Reputation: 7445

Is there any way to set type of ViewBag item?

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

Upvotes: 0

Views: 2688

Answers (3)

Asif Mushtaq
Asif Mushtaq

Reputation: 13150

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

Upvotes: 4

Christofer Eliasson
Christofer Eliasson

Reputation: 33865

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;

Upvotes: 2

Raphaël Althaus
Raphaël Althaus

Reputation: 60493

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

Upvotes: 1

Related Questions