-1

I am trying to use an @Html.ActionLink for a clickable link in my view, however I get a run time exception:

System.ArgumentException: 'Value cannot be null or empty. Parameter name: linkText

The first param of Html.ActionLink is linkText, and as you can see from my code below, it is indeed not null or empty.

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

Below is a screen shot of the page, at the top I embedded a @(string.IsNullOrWhiteSpace(o.Person.FirstName) ? "null/ws": o.Person.FirstName) as you can see, it is indeed not null or empty.

enter image description here

Per request, below is the entire portion of the cshtml file that pertains to this issue.

<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>
Jacked_Nerd
  • 209
  • 1
  • 9
  • 1
    Clearly, FirstName *is* null or empty. I don't understand how we can see from your code that it isn't. Can you elaborate? – Lasse V. Karlsen Apr 24 '19 at 14:18
  • It shouldn't be. I handled that on the back end, and it someone was null it should have created a new Person with the name NO NAME. Also it is displaying elsewhere on the page.. I can add some null checking and see if this works. – Jacked_Nerd Apr 24 '19 at 14:21
  • Could it be something regarding deserialization, that you did indeed return a person with a first name from the backend, but the frontend somehow didn't deserialize properly so it doesn't have it? – Lasse V. Karlsen Apr 24 '19 at 14:22
  • @LasseVågsætherKarlsen This is possible, although I am not sure how I can confirm that? Any Ideas? – Jacked_Nerd Apr 24 '19 at 14:29
  • I would still try to simply output the text to the page with something like `@(string.IsNullOrWhiteSpace(o.Person.FirstName) ? "null/ws" : o.Person.FirstName)` and see what it outputs. – Lasse V. Karlsen Apr 24 '19 at 14:30
  • I still want to know how we can see from your code that it isn't null or empty. I think you have some assumptions about what your code is supposed to be doing that doesn't hold true. There is no way for us to see from your code what the content of those objects look like, there's no guarantee obvious in that code that this property holds value. Well, other than the fact that it throws an exception at runtime which indicates quite clearly that it doesn't. – Lasse V. Karlsen Apr 24 '19 at 14:31
  • @LasseVågsætherKarlsen @(string.IsNullOrWhiteSpace(o.Person.FirstName) ? "null/ws" : o.Person.FirstName) just produced on my screen: NOT NOT NOT Nick Nick NOT NOT Nick NOT NOT NOT NOT NOT NOT NOT Connor Connor Trina Trina Connor John Billie Ramadugula Seiji Mary Trina Jerome Sonya Latanya Lonnie Lovella Leonardo Trina Fabina Billie Seiji Sonya Seiji Leonardo Shelby Leonardo Trina Douglas Connor Kate Kristin KRISTI Fabina Fabina Jay Brian Warren Seiji Alex John Ramadugula Fabina Seiji Seiji Sonya Sonya Connor Billie Sonya Billie Mary John NOT Kristin Connor . . . . . – Jacked_Nerd Apr 24 '19 at 14:34
  • Just produced what? Nothing? – Lasse V. Karlsen Apr 24 '19 at 14:35
  • @LasseVågsætherKarlsen check my OP, I posted the results of @(string.IsNullOrWhiteSpace(o.Person.FirstName) ? "null/ws" : o.Person.FirstName) to show you – Jacked_Nerd Apr 24 '19 at 14:39
  • You should check all cshtml file, may be it show exception at line of code before – Hien Nguyen Apr 24 '19 at 14:42
  • @LasseVågsætherKarlsen you were right, somewhere within the 233 results I had a null.. even though I thought I had it accounted for.... – Jacked_Nerd Apr 24 '19 at 15:18
  • Good that you found it, one less bug on the internet :) – Lasse V. Karlsen Apr 29 '19 at 07:32
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ian Kemp Apr 29 '19 at 07:33

4 Answers4

1

I tried to reproduce it. You need check null before using o?.Person?.FirstName and o?.Person?.IDPerson If C# 5 you can use o.Person.FirstName != null ? o.Person.FirstName : "Default Value" This code worked

  <td>@Html.ActionLink(o?.Person?.FirstName, "Detail", "Player", 
new { selectedPlayerID = o?.Person?.IDPerson, referringController = "ValidationHistory" }, null)</td>
Hien Nguyen
  • 21,001
  • 7
  • 35
  • 48
1
  1. First check Model null or not,Then after check o.Person.FirstName null or not.

            ## Check Model Like: ##
    
             - @if(Model != null && Model.Count() != 0)
                {
                  foreach (var o in Model.Results)
                  {
                    ---------
                  }
                }
            ## Check Record Like: ##
            if(o.Person!=null)
            {
             -----------
            }
    
Pr0mis PAtel
  • 160
  • 1
  • 10
0

I was wrong in my original post. There were some empty strings being returned.

To solve:

                        @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>
                        }
Jacked_Nerd
  • 209
  • 1
  • 9
0

See, You need to check first in loop o.Person this 'Person' object is null or not and other is inside Person object FirstName is null or not, you can handle it like this:

 if(o.Person!=null)
    {
        @Html.ActionLink(o.Person.FirstName ?? "Not Found", "Detail", "Player", new { selectedPlayerID = o.Person.IDPerson, referringController = "ValidationHistory" }, null)
    }
Abhay Agarwal
  • 101
  • 1
  • 9