ViewModels or ViewBag?

Basically, Should everything be done through the viewmodel or is it Ok to also incorporate viewbag? Everything should be done inside a view model. That’s what a view model is. A class that you specifically define to meet the requirements of your view. Don’t mix ViewBags with ViewModels. It is no longer clear for the … Read more

How do I access ViewBag from JS

if you are using razor engine template then do the following in your view write : <script> var myJsVariable=”@ViewBag.MyVariable” </script> UPDATE: A more appropriate approach is to define a set of configuration on the master layout for example, base url, facebook API Key, Amazon S3 base URL, etc …“` <head> <script> var AppConfig = @Html.Raw(Json.Encode(new … Read more

ViewBag/ViewData Lifecycle

The data you put in the ViewBag/ViewData is only available during the life-cycle of the request within which you populated it. MVC does not have post backs. If you need something to persist over more than a single request, you should use Session. Here is a decent article about the differences between ViewData, ViewBag, and … Read more

How to use a ViewBag to create a dropdownlist?

You cannot used the Helper @Html.DropdownListFor, because the first parameter was not correct, change your helper to: @Html.DropDownList(“accountid”, new SelectList(ViewBag.Accounts, “AccountID”, “AccountName”)) @Html.DropDownListFor receive in the first parameters a lambda expression in all overloads and is used to create strongly typed dropdowns. Here’s the documentation If your View it’s strongly typed to some Model you … Read more

Modifying MVC 3 ViewBag in a partial view does not persist to the _Layout.cshtml

If you pass the ViewBag into the partial’s viewdatadictionary, then pull it out (and cast), you can do whatever you want and the reference is kept. The cool part is that since it’s dynamic, you can even add properties and then they’ll show up on the parent page’s Viewbag. Page: //set the viewbag into the … Read more

Can’t access ViewBag in a partial view in ASP.NET MVC3

That should work without any problems. In my HomeController Index action I add a message to the ViewBag: ViewBag.Message = “Welcome to ASP.NET MVC!”; On the Index View I add the partial view: @Html.Partial(“ViewName”) And on the partial view I render the message: @ViewBag.Message From the comments below: there seems to be a problem when … Read more

The name ‘ViewBag’ does not exist in the current context

I was having the same problem. Turned out I was missing the ./Views/Web.config file, because I created the project from an empty ASP.NET application instead of using an ASP.NET MVC template. For ASP.NET MVC 5, a vanilla ./Views/Web.config file contains the following: <?xml version=”1.0″?> <!– https://stackoverflow.com/a/19899269/178082 –> <configuration> <configSections> <sectionGroup name=”system.web.webPages.razor” type=”System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=3.0.0.0, Culture=neutral, … Read more