+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / Unable to use ViewBag in Views and Controllers

Unable to use ViewBag in Views and Controllers

2 posts, 0 answered
  1. Samir Vaidya
    Samir Vaidya avatar
    261 posts
    Registered:
    05 Feb 2005
    21 Feb 2013
    Link to this post
    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.

  2. Stanislav Velikov
    Stanislav Velikov avatar
    1113 posts
    Registered:
    08 Dec 2016
    25 Feb 2013
    Link to this post
    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
2 posts, 0 answered