在索引視圖中我想檢查對象的參數是否爲空。 我已經做了下面的代碼。檢查剃鬚刀視圖中的對象參數是否爲空 - 索引?
<td>
@if (String.IsNullOrEmpty(item.Account.Name) == false)
{
@Html.ActionLink(item.Account.Name, "../Accounts/Details", new { id = item.Account.AccountID })
}
else
{
@Html.DisplayFor(modelItem => item.Account.Name)
}
</td>
而且我得到這個錯誤:
{"Object reference not set to an instance of an object."}
所以我應該怎麼檢查,如果在空參數?
1-ActionLink如果對象爲空但DisplayFor不會生成錯誤。
2 - 這是在索引視圖中的foreach循環。
因爲'Account'是'null'(你需要先檢查一下) –
你綁定了視圖嗎? –
@StephenMuecke非常感謝你。 –