【发布时间】:2015-07-20 11:22:22
【问题描述】:
我有一个类允许将某些属性留空(null)问题是我不知道如何正确检查并不断收到错误。
这是课程:
public class ZoomListItem
{
public string image { get; set; }
public string text1 { get; set; }
public string text2 { get; set; }
public ZoomAction action { get; set; }
}
这是我使用实例的方式:
@foreach (ZoomListItem item in Model.templates)
{
var actionUrl = (item.action != null) ? "" : "#"; //error here
类的实例化没有赋值动作值,只有文本和图像被赋值。我在条件行中收到以下错误:
System.Web.Mvc.dll 中出现“System.ArgumentException”类型的异常,但未在用户代码中处理
附加信息:值不能为空或为空
我是否没有正确检查 item.action != null?
【问题讨论】:
-
如果您尝试检查“item”是否为空???因为乍一看,您的代码似乎是正确的。
-
@Dryadwoods 如果 item 为 NULL,我们会得到一个 NRE 而不是上面提到的那个。但是,你确定,你在这一行得到了错误
-
ZoomAction可能是struct吗? -
Value cannot be null or empty但您将actionUrl设置为""- 一个空字符串。我假设您稍后在不允许使用空字符串的地方使用actionUrl,并且错误实际上并没有发生在您标记的行上(它不能在那里发生 - 如果你真的在使用自动属性,也就是说)。 -
@HimBromBeere:那是剃刀语法。 @ 基本上说这是代码,而不是我想出现在页面上的东西。