c# - Evaluate value from web.config file via ViewBag -
the task simple. have value in web.config, this:
<add key="menutypes" value="1,2,3"/> and in razor file need render menus based on value. if have value 3 in config file (menutypes), want render item. , if don't have it, not want render it:
<div> <ul id="setthemup" class="vehicles"> <li><a class="bikes" href="#1">bikes</a></li> <li><a class="cars" href="#2">cars</a></li> @if (viewbag.menutypes(((int)3).tostring()) > -1) { <li><a class="trucks" href="#3">trucks</a></li> } </ul> i have tried:
<div> <ul id="setthemup" class="vehicles"> <li><a class="bikes" href="#1">bikes</a></li> <li><a class="cars" href="#2">cars</a></li> @if (viewbag.menutypes.contains("3")) { <li><a class="trucks" href="#3">trucks</a></li> } </ul> </div> but on both cases getting: runtimebinderexception unhandled user code cannot perform runtime binding on null reference.
i got work this:
<add key="trucksenabled" value="true"/> <div> <ul id="setthemup" class="tabs"> <li><a class="bikes" href="#1">bikes</a></li> <li><a class="cars" href="#2">cars</a></li> @if ((bool)viewbag.trucksenabled) { <li><a class="trucks" href="#3">trucks</a></li> } </ul> </div> but cant use way. need work in first example. not razor/mvc. appreciated.
controller
var menutypes = webconfigurationmanager.appsettings["menutypes"].tostring()) string[] tokens = menutypes.split(','); //convert strings array viewbag.menutypes = tokens; view -- menutypes list, can use contains():
@if (viewbag.menutypes.contains("3")) { <li><a class="trucks" href="#3">trucks</a></li> }
Comments
Post a Comment