0

Submitting a POST ajax call to a controller with an array parameter.

I have a parameter array,
I have a static array that a I use to check the parameter array against.
I have a third array created using the .Except method to create an array that is everything but the parameter values.

The POST ajax call works like it should. I can return and see the values I am sending to it. That's what I'm doing with that quick TempData. So, I know the parameter is not empty.

Here is the controller:

    [HttpPost]
    public ActionResult MyAction(string[] subLineNames)
    {
       //Static array to check against parameter
        string[] sublineArray = new string[] { "BI/PD", "Hired", "Non-Owned", "PIP", "Addtl-PIP", "Medical Payments", "UM PD", "UM CSL", "UIM CSL", "Terrorism" };

        //Create new array for all minus the values in the parameter
        /* The error happens here. The .Trim is causing some issue I can't see.  */
        /* I know that jquery ajax call is sending a bunch of white space, so I use trim to get rid of the white space characters. */
        string[] DifferArray = sublineArray.Except(subLineNames.Select(m => m.Trim())).ToArray();

        //Test to ensure the array parameter is not empty. (it works and brings back what I sent to it)
        if (subLineNames != null)
        {
            for (int i = 0; i < subLinesNames.Length - 1; i++)
            {
                TempData["AA"] += subLineNames[i];
            }
        }

    }

Frustrating because I had this working prior. I didn't change anything that would cause it to now do this. Any help would be so appreciated.

swapmeet_Lou
  • 282
  • 2
  • 22
  • 1
    Does this answer your question? [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) – gunr2171 Nov 05 '19 at 18:43
  • @swapmeet_Lou What are you trying to accomplish by your ajax? Are you trying to change the View? You're returning a string `"Testing"` from your controller. – Ryan Wilson Nov 05 '19 at 18:43
  • On what line and which variable does it happen to? Probably subLineNames is null – the_lotus Nov 05 '19 at 18:46
  • @Ryan Wilson. I am submitting these values to a table i have set up. I left it out of the controller because that's not where to the problem is. Plus, if I can't get this to work in this simplistic manner, I can't insert them into the table. This was literally working a week ago and something I did caused this to screw up. I just can't see why. Even at this most basic level – swapmeet_Lou Nov 05 '19 at 18:48
  • gunr2171 no.. that doesn't answer my question. I know I stated it in my answer above, the sublineNames parameter isn't null. I can show that it is not null by just passing the values back to me as a test. – swapmeet_Lou Nov 05 '19 at 18:49
  • @the_lotus it specifically errors out on the string[] DifferArray = sublineArray.Except(subLineNames.Select(m => m.Trim())).ToArray(); I check the subLineNames each time and it has the values I send to it. So it's not null – swapmeet_Lou Nov 05 '19 at 18:51
  • 2
    If an element of `subLineNames` contains a `null` value, then you try to call `.Trim()` on it.. ker-pow. Without seeing the actual values there's no way to be certain. I'd set a breakpoint on this method and examine what was actually there.. then post that here so we can be of more help. Lastly, your action method doesn't return anything. – Sam Axe Nov 05 '19 at 19:06
  • @Sam Axe the values I send are the same values in the string[] sublineArray = new string[] { "BI/PD", "Hired", "Non-Owned", "PIP", "Addtl-PIP", "Medical Payments", "UM PD", "UM CSL", "UIM CSL", "Terrorism" }; I just display them in a checkbox and send the selected checkbox values to the controller. So maybe somewhere in there I am including a null value that I just can't see .. – swapmeet_Lou Nov 05 '19 at 19:16
  • @swapmeet_Lou: ah ha! Are you using either `@Html.CheckboxFor` or `@Html.Checkbox`? If so, unchecked boxes will definitely insert null values. You can see this by setting a breakpoint, or using a debugging proxy like Fiddler, or using some trivial Linq to get a count of null values. Use the browser debugging tools (Inspect Element) on a checkbox if you want to see why you get null values. – Sam Axe Nov 05 '19 at 19:20
  • @Sam Axe, so I was wrong in saying there are no null values. I checked the jquery push values before I send and there are null values there form the checkbox inputs. What boggles me is just over a week ago this exact thing was working. Moved on to another part and came back to it today.. Now this.. :( To answer your question though, I'm just using an input .. not checkboxfor. still Null values like everyone has said though. – swapmeet_Lou Nov 05 '19 at 19:24
  • @swapmeet_Lou: I'm glad you found it! – Sam Axe Nov 05 '19 at 19:25

1 Answers1

3

Perhaps you need to null-check the elements in your parameter array before calling .Trim() on them:

string[] DifferArray = sublineArray.Except(subLineNames.Where(m => !string.IsNullOrWhiteSpace(m)).Select(m => m.Trim())).ToArray();

Better yet, you can store a reference to your sanitized parameter array:

[HttpPost]
public ActionResult MyAction(string[] subLineNames)
{
    if (subLineNames == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest, $"You must provide {nameof(subLineNames)}.");
    }

    var sanitizedSubLineNames = subLineNames.Where(m => !string.IsNullOrWhiteSpace(m)).Select(m => m.Trim());
    var sublineArray = new string[] { "BI/PD", "Hired", "Non-Owned", "PIP", "Addtl-PIP", "Medical Payments", "UM PD", "UM CSL", "UIM CSL", "Terrorism" };
    var differArray = sublineArray.Except(sanitizedSubLineNames).ToArray();

    // Do something...
}
Tom Faltesek
  • 2,768
  • 1
  • 19
  • 30
  • Thank you. the .Where(m => !string.IsNullOrWhiteSpace(m)) was what solved it for me. I was definitely wrong in my first NOT NULL assessment, but I can't for the life of me figure out how it happened and where I could have gone to find that answer to solve it. I was telling Sam above that I send checkbox values to the controller and checked to see what I was actually sending. I will dive deeper into the sanitized reference you gave as well. Thank you. – swapmeet_Lou Nov 05 '19 at 19:38
  • Glad you figured out the issue. Cheers. – Tom Faltesek Nov 05 '19 at 19:40