how i can display "Name - LastName" with ViewBag?
<h2> @{ViewBag.Name;} - @{ViewBag.LastName;} </h2>
I have error
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.
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.
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).
I don't know what you pass via the ViewBag, but do remove the brackets
<h2> @ViewBag.Name - @ViewBag.LastName </h2>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With