不寻常的"值不能为空"

时间:2015-05-13 19:58:59

标签: c# asp.net-mvc

我在托管我的网站的服务器上有一个不寻常的问题(在本地工作就好了)。

在发布请求中,我将一些值传递给列表,以防我从模型中收到错误消息无效,因此数据可以在用户进行更改时保留在屏幕上(数据I&#39 ; m指的是在表格中,与表格相同的页面上)

@if (Model.IncomeExpensesViewModel.LastSixIncomeExpenses.Count() > 0)
{
 for (var i = 0; i < Model.IncomeExpensesViewModel.LastSixIncomeExpenses.Count(); i++)
 {
 @Html.HiddenFor(m => m.IncomeExpensesViewModel.LastSixIncomeExpenses[i].Sum, new { @Value = Model.IncomeExpensesViewModel.LastSixIncomeExpenses[i].Sum })
 @Html.HiddenFor(m => m.IncomeExpensesViewModel.LastSixIncomeExpenses[i].Tag, new { @Value = Model.IncomeExpensesViewModel.LastSixIncomeExpenses[i].Tag })
 @Html.HiddenFor(m => m.IncomeExpensesViewModel.LastSixIncomeExpenses[i].Date, new { @Value = Model.IncomeExpensesViewModel.LastSixIncomeExpenses[i].Date })
 @Html.HiddenFor(m => m.IncomeExpensesViewModel.LastSixIncomeExpenses[i].Type, new { @Value = Model.IncomeExpensesViewModel.LastSixIncomeExpenses[i].Type })
  }
}

因此,当我提交输入信息时,我收到错误

Value cannot be null. Parameter name: source

在这一行

@if (Model.IncomeExpensesViewModel.LastSixIncomeExpenses.Count() > 0)

Model.IncomeExpensesViewModel.LastSixIncomeExpensesList<SingleIncomeExpenseViewModel>

我尝试使用Count()和Any()。它没有用。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您必须将语句更改为@if (Model.IncomeExpensesViewModel.LastSixIncomeExpenses != null && Model.IncomeExpensesViewModel.LastSixIncomeExpenses.Count() > 0),因为您无法枚举空集合。

.Any()相同 - 它不会检查集合是否为空,因此在这种情况下抛出异常。