Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 637
CADAVID Avatar asked Mar 23 '12 01:03

CADAVID


People also ask

Can we use ViewBag to pass data from view to controller?

ViewBag itself cannot be used to send data from View to Controller and hence we need to make use of Form and Hidden Field in order to pass data from View to Controller in ASP.Net MVC Razor.

How do I use ViewBag in Web API?

ViewBag: A ViewBag is used for passing data from a controller to a view. It is a dynamic feature of C#. ViewBag does require typecasting at the time of passing the object to the class. ViewData: A ViewData is also used for passing data from the controller to the view.

How does ViewBag work in MVC?

In general, ViewBag is a way to pass data from the controller to the view. It is a type object and is a dynamic property under the controller base class. Compared to ViewData, it works similarly but is known to be a bit slower and was introduced in ASP.NET MVC 3.0 (ViewData was introduced in MVC 1.0).


1 Answers

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

<h2> @ViewBag.Name - @ViewBag.LastName </h2>
like image 194
AFD Avatar answered Sep 28 '22 09:09

AFD