【发布时间】:2020-01-09 02:56:18
【问题描述】:
如何在将 Values 分配给变量之前检查其是否包含参数?
null 检查似乎不起作用
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 = ;
}
【问题讨论】:
-
你应该移动
var tempvalue = @ViewContext.RouteData.Values["offeringId"] -
删除
ViewContext.RouteData.Values["offeringId"]之前的@。另外,如果值是 null 那为什么还要把它赋给 null 呢?
标签: javascript asp.net-mvc model-view-controller razor