Html ActionLink运行时错误:'值不能为null或为空。但值不为null或为空

问题描述 投票:-1回答:4

我试图在我的视图中使用@Html.ActionLink作为可点击链接,但是我得到了运行时异常:

System.ArgumentException:'值不能为null或为空。参数名称:linkText

Html.ActionLink的第一个参数是linkText,正如你从下面的代码中看到的那样,它确实不是空的或空的。

                    @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或空的。

enter image description here

根据请求,下面是与此问题相关的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>
c# asp.net-mvc
4个回答
1
投票

我试图重现它。在使用o?.Person?.FirstNameo?.Person?.IDPerson之前需要检查null如果C#5你可以使用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>

1
投票
  1. 首先检查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) { ----------- }

0
投票

我原来的帖子里错了。有一些空字符串被返回。

要解决:

                        @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>
                        }

0
投票

看,你需要先检查循环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)
    }
© www.soinside.com 2019 - 2024. All rights reserved.