c# - Clear the ViewBag? -


is there way clear viewbag?

viewbag has no setter, can't nulled out:

viewbag = null; 

i can't seem use reflection iterate on , wipe out dynamic properties, can't create instance of dynamic.

note: know viewbag on own bit of code smell it's not typed, , giant collection of global variables. , we're moving away it, still need deal in meantime.

you can call

viewdata.clear(); 

as viewbag uses internally.

here working example - https://dotnetfiddle.net/gmxcti . if uncomment commented line, displayed text cleared

here current implementation viewbag in mvc:

public dynamic viewbag {         {         if (_dynamicviewdatadictionary == null)         {             _dynamicviewdatadictionary = new dynamicviewdatadictionary(() => viewdata);         }         return _dynamicviewdatadictionary;     } } 

and part of dynamicviewdatadictionary

// implementing function improves debugging experience provides debugger list of // properties defined on object public override ienumerable<string> getdynamicmembernames() {     return viewdata.keys; }  public override bool trygetmember(getmemberbinder binder, out object result) {     result = viewdata[binder.name];     // since viewdatadictionary returns result if key not exist, return true     return true; }  public override bool trysetmember(setmemberbinder binder, object value) {     viewdata[binder.name] = value;     // can set key in dictionary return true     return true; } 

so can see depends on viewdata object


Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -