[英]Html ActionLink run time error: 'Value cannot be null or empty. but value is not null or empty
我試圖將@Html.ActionLink
用於視圖中的可點擊鏈接,但是出現運行時異常:
System.ArgumentException:'值不能為null或為空。 參數名稱:linkText
Html.ActionLink
的第一個參數是linkText
,正如您從下面的代碼中看到的那樣,它的確不是null或為空。
@foreach (var o in Model.Results)
{
<tr>
<td>@Html.ActionLink(o.Person.FirstName, "Detail", "Player", new { selectedPlayerID = o.Person.IDPerson, referringController = "ValidationHistory" }, null)</td>
<td>@o.Person.FirstName</td>
<td>@o.Person.LastName</td>
<td>@o.Person.SocialSecurityNumber</td>
下面是該頁面的屏幕截圖,在頂部,我嵌入了@(string.IsNullOrWhiteSpace(o.Person.FirstName)?“ null / ws”:o.Person.FirstName),正如您所看到的,它的確不是null或為空。
根據每個請求,以下是與該問題有關的cshtml文件的整個部分。
<div class="col-md-10 text-center">
@if (Model.Results.Count == 0)
{
<h3>No Data to Display</h3>
<h3>Please Try Different Search Parameters</h3>
}
else
{
<div id="tablecontaner" class="col-md-10 text-left">
<table id="PVSearchReport" class="table table-striped table-bordered" style="width:100%;padding:10px">
<thead>
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>SSN</th>
<th>IRS / TIN</th>
<th>DMF Details</th>
<th>List Matches</th>
<th>Executed At</th>
<th>Executed By</th>
<th>Club ID</th>
<th>Lists</th>
</tr>
</thead>
<tbody>
@foreach (var o in Model.Results)
{
<tr>
<td>@Html.ActionLink(o.Person.FirstName, "Detail", "Player", new { selectedPlayerID = o.Person.IDPerson, referringController = "ValidationHistory" }, null)</td>
<td>@o.Person.FirstName</td>
<td>@o.Person.LastName</td>
<td>@o.Person.SocialSecurityNumber</td>
<td>@o.Validation_Results.IRSOk</td><!--IRS/TIN-->
<td>@o.Validation_Results.DMFOk</td><!--DMF Details-->
<td>@o.Validation_Results.ListOk</td><!--List Matches-->
<td>@o.Validation_Results.ExecutedAt<!--Executed At-->
<td>@o.Validation_Results.ExecutedBy</td><!--Executed By-->
<td>@o.Person.ClubID</td>
<td>@o.ListMatches</td>
</tr>
}
</tbody>
</table>
</div>
}
</div>
我試圖重現它。 使用o?.Person?.FirstName
和o?.Person?.IDPerson
之前,您需要檢查null是否使用C#5,則可以使用o.Person.FirstName != null ? o.Person.FirstName : "Default Value"
o.Person.FirstName != null ? o.Person.FirstName : "Default Value"
此代碼有效
<td>@Html.ActionLink(o?.Person?.FirstName, "Detail", "Player",
new { selectedPlayerID = o?.Person?.IDPerson, referringController = "ValidationHistory" }, null)</td>
首先檢查Model是否為 null,然后再檢查o.Person.FirstName是否為 null。
## Check Model Like: ## - @if(Model != null && Model.Count() != 0) { foreach (var o in Model.Results) { --------- } } ## Check Record Like: ## if(o.Person!=null) { ----------- }
我在原始帖子中寫錯了。 返回了一些空字符串。
解決:
@if (o.Person.FirstName == null || o.Person.FirstName == "" || o.Person.FirstName == "NOT")
{
<td>Not Found</td>
}
else
{
<td>@Html.ActionLink(o.Person.FirstName, "Detail", "Player", new { selectedPlayerID = o.Person.IDPerson, referringController = "ValidationHistory" }, null)</td>
}
請參閱,您需要首先在循環o.Person中檢查此“ Person”對象是否為null,其他是否在Person對象中FirstName是否為null,您可以按以下方式進行處理:
if(o.Person!=null)
{
@Html.ActionLink(o.Person.FirstName ?? "Not Found", "Detail", "Player", new { selectedPlayerID = o.Person.IDPerson, referringController = "ValidationHistory" }, null)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.