Unable to use ViewBag in Views and Controllers

Posted by Community Admin on 04-Aug-2018 03:51

Unable to use ViewBag in Views and Controllers

All Replies

Posted by Community Admin on 21-Feb-2013 00:00

I just added some MVC Views and Controllers to my Sitefinity Web Application project which uses the ViewBag element, but when I attempt to access any of these pages, I receive the following error message:

Compiler Error Message: CS0103: The name 'ViewBag' does not exist in the current context

How do I get the ViewBag element to render correctly within my Sitefinity MVC Views and Controllers?


Please advise.

Posted by Community Admin on 25-Feb-2013 00:00

Hi,

Define ViewBag value in the controller method action

public ActionResult Index()
      
          var model = new ViewBagTestModel();
          if (string.IsNullOrEmpty(this.Message))
          
              model.Message = "Hello, World!";
              ViewBag.Test = "This is text form Viewbag";
          

and in the view call ViewBag with its key for access in my case its "Test";

@   
  
    @ViewBag.Test
  


Kind regards,
Stanislav Velikov
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

This thread is closed