CADAVID
CADAVID

Reputation: 93

How can I use ViewBag in the View

how i can display "Name - LastName" with ViewBag?

<h2> @{ViewBag.Name;} - @{ViewBag.LastName;} </h2>

I have error

Upvotes: 5

Views: 28877

Answers (3)

Happy
Happy

Reputation: 1797

<h2> @ViewBag.Name - @ViewBag.LastName </h2>

avoid the extra { and you are good

Upvotes: 0

Shyju
Shyju

Reputation: 218732

<h2> @ViewBag.Name - @ViewBag.LastName </h2>

you only need @.

I strongly suggest you to start using strongly typed views instead of using ViewBag. strongly typed views keeps your views ( and code ) clean and maintainable.

Upvotes: 2

Andrei Drynov
Andrei Drynov

Reputation: 8592

I don't know what you pass via the ViewBag, but do remove the brackets

<h2> @ViewBag.Name - @ViewBag.LastName </h2>

Upvotes: 10

Related Questions