1

I used to access the viewbag in view as below:

@ViewBag.myfield

or

@Html.Raw(Viewbag.myfield)

EDIT: here is the controller code:

public class myController: Controller
{
    public ActionResult Index()
    {
        ViewBag.myfield = "Hello";
        return View();
    }
}

but now using mvc 5 none of the above seem to work which probably means it has changed.Am I doing anything wrong or is it changed?

thanks for your help.

Edit: I've solved this by closing the Visual Studio and deleting the user file and then it was fixed. It was mentioned in the duplicate question link above.

arash moeen
  • 4,533
  • 9
  • 40
  • 85
  • 2
    What do you mean with "not working"? Your code seems fine, what is the error or result? – Sefa Dec 31 '14 at 13:23
  • 1
    Please share the controller code also, no specific changes in MVC 5 related with View bag as far as I know. – ssilas777 Dec 31 '14 at 13:23
  • Can you show us the controller's code? – Tushar Gupta Dec 31 '14 at 13:23
  • I've added the controller code which is a sample, but when I add @ in my view it doesn't have ViewBag or evern Html in the suggested commands, seems like they are missing. – arash moeen Dec 31 '14 at 13:27
  • Nothing in ASP.NET MVC 5 has changed regards using `ViewBag` in views. Can you be more specific about what you mean by "not working" please? – Jason Evans Dec 31 '14 at 13:27
  • @JasonEvans before I used to type @ and press Ctrl + Space for suggested commands and I could easily pick ViewBag or Html or Url to complete but now when I press Ctrl + Space those are gone and I can't access them – arash moeen Dec 31 '14 at 13:28
  • _"when I add @ in my view it doesn't have ViewBag or evern Html in the suggested commands"_ - did this work before? What edition of Visual Studio? Have you installed MVC through the Web Platform Installer? Did you restart Visual Studio? – CodeCaster Dec 31 '14 at 13:28
  • This might help : http://stackoverflow.com/questions/12041633/razor-views-not-seeing-system-web-mvc-htmlhelper Looks like you might be missing a config. – Jason Evans Dec 31 '14 at 13:29
  • @CodeCaster before I used mvc 4 using VS 2012, now I'm using mvc 5 using VS 2013, it came with the studio itself. – arash moeen Dec 31 '14 at 13:30
  • Yeah then see duplicate or any of the other many questions that come up for ["visual studio 2013 intellisense mvc"](http://stackoverflow.com/search?q=Visual+studio+2013+intellisense+mvc). Please try to search first, and explain what you have tried. – CodeCaster Dec 31 '14 at 13:32
  • 1
    @CodeCaster Thank you for the link and sorry about not checking the duplicate first, I wasn't sure of what keyword I should be searching for. I'll edit my question with what I did to fix it. – arash moeen Dec 31 '14 at 13:41
  • 1
    Great you got it figured out, no problem. And thanks for reporting back. :) – CodeCaster Dec 31 '14 at 13:41

0 Answers0