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

时间:2019-04-24 13:47:24

标签: c# asp.net-mvc

我试图将@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或为空。

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>

4 个答案:

答案 0 :(得分:1)

  1. 首先检查型号是否为空,然后检查              o.Person.FirstName 为空。

    z[0] = 2 < n

答案 1 :(得分:0)

我试图复制它。 使用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>

答案 2 :(得分: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>
                        }

答案 3 :(得分: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)
    }