javascript - Check ViewContext.RouteData.Values to see if parameter exists
问题描述
How can I check to see if Values contains a parameter before assigning it to a variable?
null checks do not seem to work
var offeringId;
if (@ViewContext.RouteData.Values["offeringId"] == null) {
offeringId = null;
} else {
offeringId = @ViewContext.RouteData.Values["offeringId"];
}
//the above line translates to the code below in the console when executed.
//If the value doesn't exist, nothing is displayed on the left side of the = operator.
var offeringId;
if ( == null) {
offeringId = null;
} else {
offeringId = ;
}
解决方案
首先这样做:
@{ string offeringValue = ViewContext.RouteData.Values["offeringId"] ?? "null";}
然后像这样分配offeringId
:
var offeringId = @offeringValue;
如果ViewContext.RouteData.Values["offeringId"] == null
,您的脚本将如下所示:
var offeringId = null;
??
是null
合并运算符(它将返回第一个不是 的值null
)
推荐阅读
- java - 方法重载和原始类型如何工作?
- javascript - 我无法写入 DIV 对象中的所有项目
- html - 无法在里面重复获取的活动类
- 元素
- gradle - Flutter Assemble debug linkng error app will not compile,尝试了一切
- macos - 从 launchd / launctl 运行 osascript (AppleScript)
- elixir - Elixir - 超时同时接收和发送消息
- node.js - 我们可以在MongoDB的对象集合中插入数据吗
- angular - 如何解决这个问题(“this.http.post(...).map(...).concatMap 不是函数”)?
- java - 如何在android应用程序中集成大地水准面模型计算?
- mysql - 数据库升级建议