Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear the ViewBag?

Is there any way to clear the ViewBag?

ViewBag has no setter, so it can't simply be nulled out:

ViewBag = null;

I also can't seem to use reflection to iterate over it and wipe out its dynamic properties, as you can't create an instance of a dynamic.

Note: I know ViewBag on its own is a bit of a code smell as it's not strongly typed, and is basically a giant collection of global variables. And we're moving away from it, but still need to deal with it in the meantime.

like image 318
Jerad Rose Avatar asked Mar 16 '15 20:03

Jerad Rose


People also ask

What is a ViewBag?

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).

How do you show ViewBag value in view?

This can be accessed in the view like @ViewBag.Name. You can assign a primitive or a complex type object as a value to ViewBag property. You can assign any number of properties and values to ViewBag.

Where is ViewBag stored?

ViewBag lives within the current context. IN ASP.NET the context is all the objects required to fulfill an HTTP request. Session is a state management framework in ASP.NET. Session is stored in the server in memory for a variable duration.

How do I find the ViewBag value of a controller?

To pass the strongly typed data from Controller to View using ViewBag, we have to make a model class then populate its properties with some data and then pass that data to ViewBag with the help of a property. And then in the View, we can access the data of model class by using ViewBag with the pre-defined property.


1 Answers

You can just call

ViewData.Clear();

As ViewBag uses it internally.

Here is working example - https://dotnetfiddle.net/GmxctI . If you uncomment commented line, then displayed text will be cleared

Here is current implementation for ViewBag in MVC:

public dynamic ViewBag
{
    get
    {
        if (_dynamicViewDataDictionary == null)
        {
            _dynamicViewDataDictionary = new DynamicViewDataDictionary(() => ViewData);
        }
        return _dynamicViewDataDictionary;
    }
}

And part of that DynamicViewDataDictionary

// Implementing this function improves the debugging experience as it provides the debugger with the list of all
// the properties currently defined on the object
public override IEnumerable<string> GetDynamicMemberNames()
{
    return ViewData.Keys;
}

public override bool TryGetMember(GetMemberBinder binder, out object result)
{
    result = ViewData[binder.Name];
    // since ViewDataDictionary always returns a result even if the key does not exist, always return true
    return true;
}

public override bool TrySetMember(SetMemberBinder binder, object value)
{
    ViewData[binder.Name] = value;
    // you can always set a key in the dictionary so return true
    return true;
}

So as you can see it depends on ViewData object

like image 161
Sergey Litvinov Avatar answered Oct 23 '22 07:10

Sergey Litvinov