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.

11
  • 1
    Does this answer your question? What is a NullReferenceException, and how do I fix it? Commented Nov 5, 2019 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. Commented Nov 5, 2019 at 18:43
  • On what line and which variable does it happen to? Probably subLineNames is null Commented Nov 5, 2019 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 Commented Nov 5, 2019 at 18:48
  • 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. Commented Nov 5, 2019 at 19:06

1 Answer 1

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...
}
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Glad you figured out the issue. Cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.