开发者

How does one access a ViewData item which contains a special character with the ViewBag?

开发者 https://www.devze.com 2023-03-12 21:52 出处:网络
A toy exam开发者_高级运维ple, ViewData[\"rat\"] = \"Rodent\"; var blaw = ViewBag.rat; ViewData[\"dig/dug\"] =\"Game\";

A toy exam开发者_高级运维ple,

ViewData["rat"] = "Rodent";
var blaw = ViewBag.rat;

ViewData["dig/dug"] =  "Game";
// var blaw2 = Viewbag.dig/dug;

The last line of course does not work. How would I access "dig/dug" through the ViewBag?


Just use underscore (or some another allowed symbol) instead of slash. Slash is not allowed:

ViewData["dig_dug"] =  "Game";

Since ViewBag is dynamic dig/dug will be a property of dynamic object.

You can't use some special symbols(/,. etc..) within name of properties:

public string Dig\Dug {get;set;} // compilation time error
0

精彩评论

暂无评论...
验证码 换一张
取 消